Passed
Push — master ( 638587...464501 )
by JHONATAN
03:15
created

TransferRepository   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Test Coverage

Coverage 89.06%

Importance

Changes 0
Metric Value
wmc 23
dl 0
loc 157
ccs 57
cts 64
cp 0.8906
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A prepareCollection() 0 5 1
A findOneBy() 0 16 4
A findAll() 0 7 1
A find() 0 11 3
B findOneByCriteria() 0 23 6
A getClassName() 0 3 1
A findByCriteria() 0 17 3
A findBy() 0 15 3
1
<?php
2
3
namespace Vox\Webservice;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use Vox\Webservice\Exception\WebserviceResponseException;
8
use Vox\Webservice\Proxy\ProxyFactoryInterface;
9
10
/**
11
 * the transfer repository does the job of requiring data from the webservice client for the correct transfer
12
 * however this pattern should be more flexible, a future release will fix this
13
 * 
14
 * @author Jhonatan Teixeira <[email protected]>
15
 */
16
final class TransferRepository implements TransferRepositoryInterface
17
{
18
    private $transferName;
19
    
20
    /**
21
     * @var WebserviceClientInterface
22
     */
23
    private $webserviceClient;
24
    
25
    /**
26
     * @var ObjectStorageInterface
27
     */
28
    private $objectStorage;
29
    
30
    /**
31
     * @var TransferManagerInterface
32
     */
33
    private $transferManager;
34
    
35
    /**
36
     * @var ProxyFactoryInterface
37
     */
38
    private $proxyFactory;
39
    
40
    /**
41
     * @param string $transferName
42
     * @param WebserviceClientInterface $webserviceClient
43
     * @param ObjectStorageInterface $objectStorage
44
     * @param TransferManagerInterface $transferManager
45
     * @param ProxyFactoryInterface $proxyFactory
46
     */
47 21
    public function __construct(
48
        string $transferName, 
49
        WebserviceClientInterface $webserviceClient, 
50
        ObjectStorageInterface $objectStorage,
51
        TransferManagerInterface $transferManager,
52
        ProxyFactoryInterface $proxyFactory
53
    ) {
54 21
        $this->transferName     = $transferName;
55 21
        $this->webserviceClient = $webserviceClient;
56 21
        $this->objectStorage    = $objectStorage;
57 21
        $this->transferManager  = $transferManager;
58 21
        $this->proxyFactory     = $proxyFactory;
59 21
    }
60
    
61 11
    public function find($id)
62
    {
63 11
        $transfer = $this->objectStorage->fetchByParams($this->transferName, $id)
64 11
            ?? $this->proxyFactory
65 11
                ->createProxy($this->webserviceClient->get($this->transferName, $id), $this->transferManager);
66
        
67 11
        if ($transfer && !$this->objectStorage->contains($transfer)) {
68 11
            $this->objectStorage->attach($transfer);
69
        }
70
        
71 11
        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...
72
    }
73
74 2
    public function findAll(): Collection
75
    {
76 2
        $collection = $this->webserviceClient->cGet($this->transferName);
77
78 2
        $this->prepareCollection($collection);
79
80 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...
81
    }
82
83 3
    public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null): Collection
84
    {
85
        try {
86 3
            $collection = $this->webserviceClient->cGet($this->transferName, $criteria);
87 1
        } catch (WebserviceResponseException $exception) {
88 1
            if ($exception->getCode() == '404') {
89 1
                return new ArrayCollection();
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Doctrine\Comm...tions\ArrayCollection() returns the type Doctrine\Common\Collections\ArrayCollection 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...
90
            }
91
92
            throw $exception;
93
        }
94
95 2
        $this->prepareCollection($collection);
96
97 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...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...
98
    }
99
100 2
    public function findOneBy(array $criteria)
101
    {
102
        try {
103 2
            $collection = $this->webserviceClient->cGet($this->transferName, $criteria);
104 1
        } catch (WebserviceResponseException $exception) {
105 1
            if ($exception->getCode() == '404') {
106 1
                return null;
107
            }
108
109
            throw $exception;
110
        }
111
112 1
        $this->prepareCollection($collection);
113
114 1
        if ($collection->count() > 0) {
115 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...
116
        }
117
    }
118
119 2
    public function findByCriteria(CriteriaInterface $criteria): Collection
120
    {
121 2
        $criteria->withOperationType(CriteriaInterface::OPERATION_TYPE_COLLECTION);
122
123
        try {
124 2
            $collection = $this->webserviceClient->getByCriteria($criteria, $this->transferName);
125 1
        } catch (WebserviceResponseException $ex) {
126 1
            if ($ex->getCode() == '404') {
127 1
                return new ArrayCollection();
128
            }
129
130
            throw $ex;
131
        }
132
133 1
        $this->prepareCollection($collection);
134
135 1
        return $collection;
136
    }
137
138 2
    public function findOneByCriteria(CriteriaInterface $criteria)
139
    {
140 2
        $criteria->withOperationType(CriteriaInterface::OPERATION_TYPE_ITEM);
141
142
        try {
143 2
            $transfer = $this->webserviceClient->getByCriteria($criteria, $this->transferName);
144 1
        } catch (WebserviceResponseException $exception) {
145 1
            if ($exception->getCode() == '404') {
146 1
                return null;
147
            }
148
149
            throw $exception;
150
        }
151
152 1
        if ($transfer && !$this->objectStorage->contains($transfer)) {
153 1
            $this->objectStorage->attach($transfer);
154
        }
155
        
156 1
        if ($transfer) {
157 1
            $transfer = $this->proxyFactory->createProxy($transfer, $this->transferManager);
158
        }
159
160 1
        return $transfer;
161
    }
162
163 6
    private function prepareCollection(TransferCollection $collection)
164
    {
165 6
        $collection->setObjectStorage($this->objectStorage)
166 6
            ->setTransferManager($this->transferManager)
167 6
            ->setProxyFactory($this->proxyFactory);
168 6
    }
169
170
    public function getClassName(): string
171
    {
172
        return $this->transferName;
173
    }
174
}
175