Completed
Push — master ( bd2805...cafee2 )
by JHONATAN
02:26
created

TransferRepository   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Test Coverage

Coverage 88.71%

Importance

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