2.3.2: Create the parent iterator class

Create the parent iterator class

Create a file for the class SwiftOtter\OrderExport\Action\CollectOrderData.

In keeping with our action class pattern, CollectOrderData should have only one public method:

public function execute(
    int $orderId,
    SwiftOtter\OrderExport\Api\Data\HeaderData $headerData
) {
    // TODO: Load the order
    $output = [];
    // TODO: Accumulate output
    return $output;
}

Remember, there is nothing magical about this class or its naming. We are simply creating a self-contained unit of functionality. Any other area of the application could, in theory, easily utilize this code provided it passes in the correct data requirements.

execute takes an Order ID and a HeaderData object (which we've used to encapsulate the user-provided input). We now need to load up the order, which we'll do with a repository, injecting not a concrete class but an interface. (We'll get into the Magento service layer and the purpose of repositories in a later section.) Taking a stab at the naming for a repository responsible for orders by searching for OrderRepository will easily turn up Magento\Sales\Api\OrderRepositoryInterface. CollectOrderData will need to inject this as a dependency.

PhpStorm Tip

Rather that directly searching for OrderRepository, try taking advantage of simple code completion. Start typing that name as a type hint in your constructor, hit Enter/Return on the result in the code completion list, and PhpStorm will not only auto-complete, but will automatically add a use statement for the fully qualified class name. (Just pay careful attention to which result in the list is the type you want!)

Two methods found on almost every repository class are get (for fetching a single entity) and getList (for multiple). We simply need a single order, so use get to fetch it with our order ID, and simply store the result in a variable for now, since we have no child collectors to pass it to just yet.

Make sure execute returns a real (albeit empty) array, and all that's left for this step is to wire up SwiftOtter\OrderExport\Console\Command\OrderExport to use it. Inject CollectOrderData into OrderExport, call execute with the right input, and grab the result. So that we can continue checking the results of our work as we build our collectors, output the array results to the screen for now.

SEE THE CODE

Complete and Continue