File size: 904 Bytes
cf14c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php

declare(strict_types=1);

namespace Plugin\Application\CommandHandlers;

use Iterator;
use Plugin\Application\Commands\ListPluginsCommand;
use Plugin\Domain\PluginWrapper;
use Plugin\Domain\Repositories\PluginRepositoryInterface;

class ListPluginsCommandHandler
{
    public function __construct(
        private PluginRepositoryInterface $repo
    ) {
    }

    /**
     * @param ListPluginsCommand $cmd
     * @return Iterator<int,PluginWrapper>
     */
    public function handle(ListPluginsCommand $cmd): Iterator
    {
        $plugins = $this->repo;

        if ($cmd->type) {
            $plugins = $plugins->filterByType($cmd->type);
        }

        if ($cmd->status) {
            $plugins = $plugins->filterByStatus($cmd->status);
        }

        if ($cmd->query) {
            $plugins = $plugins->search($cmd->query);
        }

        return $plugins->getIterator();
    }
}