Completed
Pull Request — master (#394)
by Beñat
14:41
created

InMemoryUserRepository::userOfId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace Kreta\TaskManager\Infrastructure\Persistence\InMemory;
16
17
use BenGorUser\User\Infrastructure\Domain\Model\UserEventBus;
18
use Kreta\TaskManager\Domain\Model\User\User;
19
use Kreta\TaskManager\Domain\Model\User\UserId;
20
use Kreta\TaskManager\Domain\Model\User\UserRepository;
21
22
final class InMemoryUserRepository implements UserRepository
23
{
24
    private $users;
25
    private $eventBus;
26
27
    public function __construct(UserEventBus $anEventBus = null)
28
    {
29
        $this->users = [];
30
        $this->eventBus = $anEventBus;
31
    }
32
33
    public function userOfId(UserId $id)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
34
    {
35
        if (isset($this->users[$id->id()])) {
36
            return $this->users[$id->id()];
37
        }
38
    }
39
40
    public function persist(User $user)
41
    {
42
        $this->users[$user->id()->id()] = $user;
43
44
        if ($this->eventBus instanceof UserEventBus) {
0 ignored issues
show
Bug introduced by
The class BenGorUser\User\Infrastr...main\Model\UserEventBus does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
45
            $this->handle($user->events());
0 ignored issues
show
Bug introduced by
The method events() does not exist on Kreta\TaskManager\Domain\Model\User\User. Did you maybe mean recordedEvents()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
46
        }
47
    }
48
49
    private function handle($events)
50
    {
51
        foreach ($events as $event) {
52
            $this->eventBus->handle($event);
53
        }
54
    }
55
}
56