Completed
Push — master ( c5da41...e1d486 )
by Pieter
22s queued 14s
created

StoreExistingResourceEvent   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 15
c 1
b 0
f 1
dl 0
loc 66
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getEvent() 0 3 1
A setResource() 0 7 2
A getId() 0 6 2
A getResource() 0 3 1
A getRequest() 0 3 1
A __construct() 0 4 1
1
<?php
2
3
4
namespace W2w\Lib\Apie\Events;
5
6
7
use Psr\Http\Message\RequestInterface;
8
use W2w\Lib\Apie\Exceptions\InvalidReturnTypeOfApiResourceException;
9
10
/**
11
 * Event mediator for storing a resource to the data layer.
12
 */
13
class StoreExistingResourceEvent
14
{
15
    /**
16
     * @var ModifySingleResourceEvent
17
     */
18
    private $event;
19
20
    /**
21
     * @var object|null
22
     */
23
    private $resource;
24
25
    /**
26
     * @param ModifySingleResourceEvent|StoreNewResourceEvent $event
27
     */
28
    public function __construct($event)
29
    {
30
        $this->event = $event;
0 ignored issues
show
Documentation Bug introduced by
It seems like $event can also be of type W2w\Lib\Apie\Events\StoreNewResourceEvent. However, the property $event is declared as type W2w\Lib\Apie\Events\ModifySingleResourceEvent. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
31
        $this->setResource($event->getResource());
0 ignored issues
show
Bug introduced by
It seems like $event->getResource() can also be of type null; however, parameter $resource of W2w\Lib\Apie\Events\Stor...rceEvent::setResource() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

31
        $this->setResource(/** @scrutinizer ignore-type */ $event->getResource());
Loading history...
32
    }
33
34
    /**
35
     * @return string|null
36
     */
37
    public function getId(): ?string
38
    {
39
        if ($this->event instanceof  ModifySingleResourceEvent) {
0 ignored issues
show
introduced by
$this->event is always a sub-type of W2w\Lib\Apie\Events\ModifySingleResourceEvent.
Loading history...
40
            return $this->event->getId();
41
        }
42
        return null;
43
    }
44
45
    /**
46
     * @return RequestInterface
47
     */
48
    public function getRequest(): RequestInterface
49
    {
50
        return $this->event->getRequest();
51
    }
52
53
    /**
54
     * @return ModifySingleResourceEvent|StoreNewResourceEvent
55
     */
56
    public function getEvent()
57
    {
58
        return $this->event;
59
    }
60
61
    /**
62
     * @return object|null
63
     */
64
    public function getResource(): ?object
65
    {
66
        return $this->resource;
67
    }
68
69
    /**
70
     * @param object $resource
71
     */
72
    public function setResource(object $resource): void
73
    {
74
        $className = get_class($this->event->getResource());
75
        if (!$resource instanceof $className) {
76
            throw new InvalidReturnTypeOfApiResourceException(null, get_class($resource), $className);
77
        }
78
        $this->resource = $resource;
79
    }
80
}
81