ListMaterialUseCase::execute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
/**
3
 * (c) Tomasz Kunicki <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
namespace Domain\Material\UseCase\ListMaterial;
9
10
use Domain\Common\UseCase\ResponderAwareTrait;
11
use Domain\Common\UseCase\ResponderInterface;
12
use Domain\Material\Enitity\MaterialInterface;
13
use Domain\Material\Repository\MaterialRepositoryInterface;
14
15
/**
16
 * Class ListMaterialsUseCase
17
 *
18
 * @package Domain\Material\UseCase\ListMaterial
19
 */
20
class ListMaterialUseCase implements ResponderInterface
21
{
22
    use ResponderAwareTrait;
23
24
    /** @var  MaterialRepositoryInterface */
25
    private $materialsRepository;
26
27
    /**
28
     * ListMaterialsUseCase constructor.
29
     *
30
     * @param MaterialRepositoryInterface $materialsRepository
31
     */
32
    public function __construct(MaterialRepositoryInterface $materialsRepository)
33
    {
34
        $this->materialsRepository = $materialsRepository;
35
    }
36
37
    public function execute()
38
    {
39
        $materials = $this->materialsRepository->findAll();
40
        $items = $this->fetchItems($materials);
41
        $this->listFetched($items);
42
    }
43
44
    /**
45
     * @param $materials
46
     *
47
     * @return ListMaterialItem[]
48
     */
49
    private function fetchItems($materials)
50
    {
51
        $list = [];
52
        /** @var MaterialInterface $material */
53
        foreach ($materials as $material) {
54
            $unitName = null;
55
            $unit = $material->getUnit();
56
            if (!is_null($unit)) {
57
                $unitName = $unit->getName();
58
            }
59
60
            $list[] = new ListMaterialItem(
61
                $material->getId(),
62
                $material->getName(),
63
                $material->getCode(),
64
                $unitName,
65
                $material->getCategory()->getName()
66
            );
67
        }
68
69
        return $list;
70
    }
71
72
    /**
73
     * @param array $items
74
     */
75
    private function listFetched(array $items)
76
    {
77
        /** @var ListMaterialResponderInterface $responder */
78
        foreach ($this->responders as $responder) {
79
            $responder->listFetched(new ListMaterialResponse($items));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Domain\Common\UseCase\ResponderInterface as the method listFetched() does only exist in the following implementations of said interface: Domain\Material\UseCase\...ial\ListMaterialUseCase.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
80
        }
81
    }
82
}