Completed
Push — master ( 28afa9...69e2c3 )
by JHONATAN
02:50
created

TransferRepository::findAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Vox\Webservice;
4
5
use Doctrine\Common\Collections\Collection;
6
use Doctrine\Common\Persistence\ObjectRepository;
7
use Vox\Webservice\Proxy\ProxyFactoryInterface;
8
9
/**
10
 * the transfer repository does the job of requiring data from the webservice client for the correct transfer
11
 * however this pattern should be more flexible, a future release will fix this
12
 * 
13
 * @author Jhonatan Teixeira <[email protected]>
14
 */
15
final class TransferRepository implements ObjectRepository
16
{
17
    private $transferName;
18
    
19
    /**
20
     * @var WebserviceClientInterface
21
     */
22
    private $webserviceClient;
23
    
24
    /**
25
     * @var ObjectStorageInterface
26
     */
27
    private $objectStorage;
28
    
29
    /**
30
     * @var TransferManagerInterface
31
     */
32
    private $transferManager;
33
    
34
    /**
35
     * @var ProxyFactoryInterface
36
     */
37
    private $proxyFactory;
38
    
39
    /**
40
     * @param string $transferName
41
     * @param \Vox\Webservice\WebserviceClientInterface $webserviceClient
42
     * @param \Vox\Webservice\ObjectStorageInterface $objectStorage
43
     * @param \Vox\Webservice\TransferManagerInterface $transferManager
44
     * @param ProxyFactoryInterface $proxyFactory
45
     */
46 9
    public function __construct(
47
        string $transferName, 
48
        WebserviceClientInterface $webserviceClient, 
49
        ObjectStorageInterface $objectStorage,
50
        TransferManagerInterface $transferManager,
51
        ProxyFactoryInterface $proxyFactory
52
    ) {
53 9
        $this->transferName     = $transferName;
54 9
        $this->webserviceClient = $webserviceClient;
55 9
        $this->objectStorage    = $objectStorage;
56 9
        $this->transferManager  = $transferManager;
57 9
        $this->proxyFactory     = $proxyFactory;
58 9
    }
59
    
60 5
    public function find($id)
61
    {
62 5
        $transfer = $this->objectStorage->fetchByParams($this->transferName, $id)
63 5
            ?? $this->proxyFactory
64 5
                ->createProxy($this->webserviceClient->get($this->transferName, $id), $this->transferManager);
65
        
66 5
        if ($transfer && !$this->objectStorage->contains($transfer)) {
67 5
            $this->objectStorage->attach($transfer);
68
        }
69
        
70 5
        return $transfer;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $transfer also could return the type ProxyManager\Proxy\Acces...torValueHolderInterface which is incompatible with the return type mandated by Doctrine\Common\Persiste...bjectRepository::find() of object|null.
Loading history...
71
    }
72
73 2
    public function findAll(): Collection
74
    {
75 2
        $collection = $this->webserviceClient->cGet($this->transferName);
76
        
77 2
        $collection->setObjectStorage($this->objectStorage)
78 2
            ->setTransferManager($this->transferManager)
79 2
            ->setProxyFactory($this->proxyFactory);
80
        
81 2
        return $collection;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $collection returns the type Vox\Webservice\TransferCollection which is incompatible with the return type mandated by Doctrine\Common\Persiste...ctRepository::findAll() of array.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
82
    }
83
84 1
    public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null): Collection
85
    {
86 1
        $collection = $this->webserviceClient->cGet($this->transferName, $criteria);
87
        
88 1
        $collection->setObjectStorage($this->objectStorage)
89 1
            ->setTransferManager($this->transferManager)
90 1
            ->setProxyFactory($this->proxyFactory);
91
        
92 1
        return $collection;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $collection returns the type Vox\Webservice\TransferCollection which is incompatible with the return type mandated by Doctrine\Common\Persiste...ectRepository::findBy() of array.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
93
    }
94
95 1
    public function findOneBy(array $criteria)
96
    {
97 1
        $collection = $this->webserviceClient->cGet($this->transferName, $criteria);
98
        
99 1
        $collection->setObjectStorage($this->objectStorage)
100 1
            ->setTransferManager($this->transferManager)
101 1
            ->setProxyFactory($this->proxyFactory);
102
        
103 1
        if ($collection->count() > 0) {
104 1
            return $collection->first();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $collection->first() also could return the type ProxyManager\Proxy\Acces...torValueHolderInterface which is incompatible with the return type mandated by Doctrine\Common\Persiste...Repository::findOneBy() of object|null.
Loading history...
105
        }
106
    }
107
108
    public function getClassName(): string
109
    {
110
        return $this->transferName;
111
    }
112
}
113