Completed
Push — master ( 4b0171...fadee5 )
by JHONATAN
02:23
created

TransferRepository   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 61.29%

Importance

Changes 0
Metric Value
wmc 9
dl 0
loc 91
ccs 19
cts 31
cp 0.6129
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A find() 0 10 3
A findOneBy() 0 8 2
A findAll() 0 9 1
A getClassName() 0 3 1
A findBy() 0 7 1
1
<?php
2
3
namespace Vox\Webservice;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Persistence\ObjectRepository;
7
use Vox\Webservice\Proxy\ProxyFactoryInterface;
8
9
final class TransferRepository implements ObjectRepository
10
{
11
    private $transferName;
12
    
13
    /**
14
     * @var WebserviceClientInterface
15
     */
16
    private $webserviceClient;
17
    
18
    /**
19
     * @var ObjectStorageInterface
20
     */
21
    private $objectStorage;
22
    
23
    /**
24
     * @var TransferManagerInterface
25
     */
26
    private $transferManager;
27
    
28
    /**
29
     * @var ProxyFactoryInterface
30
     */
31
    private $proxyFactory;
32
    
33
    /**
34
     * @param string $transferName
35
     * @param \Vox\Webservice\WebserviceClientInterface $webserviceClient
36
     * @param \Vox\Webservice\ObjectStorageInterface $objectStorage
37
     * @param \Vox\Webservice\TransferManagerInterface $transferManager
38
     * @param ProxyFactoryInterface $proxyFactory
39
     */
40 5
    public function __construct(
41
        string $transferName, 
42
        WebserviceClientInterface $webserviceClient, 
43
        ObjectStorageInterface $objectStorage,
44
        TransferManagerInterface $transferManager,
45
        ProxyFactoryInterface $proxyFactory
46
    ) {
47 5
        $this->transferName     = $transferName;
48 5
        $this->webserviceClient = $webserviceClient;
49 5
        $this->objectStorage    = $objectStorage;
50 5
        $this->transferManager  = $transferManager;
51 5
        $this->proxyFactory     = $proxyFactory;
52 5
    }
53
    
54 3
    public function find($id)
55
    {
56 3
        $transfer = $this->proxyFactory
57 3
            ->createProxy($this->webserviceClient->get($this->transferName, $id), $this->transferManager);
58
        
59 3
        if ($transfer && !$this->objectStorage->contains($transfer)) {
60 3
            $this->objectStorage->attach($transfer);
61
        }
62
        
63 3
        return $transfer;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $transfer returns the type ProxyManager\Proxy\Acces...torValueHolderInterface which is incompatible with the return type mandated by Doctrine\Common\Persiste...bjectRepository::find() of object|null.

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...
64
    }
65
66 2
    public function findAll(): ArrayCollection
67
    {
68 2
        $collection = $this->webserviceClient->cGet($this->transferName);
69
        
70 2
        $collection->setObjectStorage($this->objectStorage)
71 2
            ->setTransferManager($this->transferManager)
72 2
            ->setProxyFactory($this->proxyFactory);
73
        
74 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...
75
    }
76
77
    public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null): array
78
    {
79
        $collection = $this->webserviceClient->cGet($this->transferName, $criteria);
80
        
81
        $collection->setObjectStorage($this->objectStorage);
82
        
83
        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 type-hinted return array.
Loading history...
84
    }
85
86
    public function findOneBy(array $criteria)
87
    {
88
        $collection = $this->webserviceClient->cGet($this->transferName, $criteria);
89
        
90
        $collection->setObjectStorage($this->objectStorage);
91
        
92
        if ($collection->count() > 0) {
93
            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...
94
        }
95
    }
96
97
    public function getClassName(): string
98
    {
99
        return $this->transferName;
100
    }
101
}
102