Owner   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 16
c 2
b 1
f 1
lcom 2
cbo 2
dl 0
loc 156
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getIdentifier() 0 4 1
A getAdapters() 0 4 1
A setAdapters() 0 4 1
A addAdapter() 0 4 1
A getCache() 0 4 1
A upload() 0 4 1
A update() 0 4 1
A remove() 0 4 1
A getAccounts() 0 8 3
A setAccount() 0 11 2
A retrieveFromCache() 0 10 2
1
<?php
2
3
namespace Libcast\AssetDistributor;
4
5
use Doctrine\Common\Cache\Cache;
6
use Libcast\AssetDistributor\Adapter\Adapter;
7
use Libcast\AssetDistributor\Adapter\AdapterCollection;
8
use Libcast\AssetDistributor\Asset\Asset;
9
10
class Owner
11
{
12
    /**
13
     *
14
     * @var string
15
     */
16
    protected $identifier;
17
18
    /**
19
     *
20
     * @var AdapterCollection
21
     */
22
    protected $adapters;
23
24
    /**
25
     * 
26
     * @var Cache
27
     */
28
    protected $cache;
29
30
    /**
31
     *
32
     * @param $identifier
33
     * @param Cache $cache
34
     * @param array $adapters
35
     */
36
    public function __construct($identifier, Cache $cache = null, array $adapters = [])
37
    {
38
        $this->identifier = $identifier;
39
        $this->adapters = new AdapterCollection($adapters);
40
        $this->cache = $cache;
41
    }
42
43
    /**
44
     *
45
     * @return string
46
     */
47
    public function getIdentifier()
48
    {
49
        return $this->identifier;
50
    }
51
52
    /**
53
     *
54
     * @return AdapterCollection
55
     */
56
    public function getAdapters()
57
    {
58
        return $this->adapters;
59
    }
60
61
    /**
62
     *
63
     * @param AdapterCollection $adapters
64
     */
65
    public function setAdapters(AdapterCollection $adapters)
66
    {
67
        $this->adapters = $adapters;
68
    }
69
70
    /**
71
     *
72
     * @param Adapter $adapter
73
     */
74
    public function addAdapter(Adapter $adapter)
75
    {
76
        $this->adapters[] = $adapter;
77
    }
78
79
    /**
80
     *
81
     * @return Cache
82
     */
83
    public function getCache()
84
    {
85
        return $this->cache;
86
    }
87
88
    /**
89
     * Upload Asset on all Adapters
90
     *
91
     * @param Asset $asset
92
     */
93
    public function upload(Asset $asset)
94
    {
95
        $this->adapters->upload($asset);
96
    }
97
98
    /**
99
     * Edit Asset on all Adapters
100
     *
101
     * @param Asset $asset
102
     */
103
    public function update(Asset $asset)
104
    {
105
        $this->adapters->update($asset);
106
    }
107
108
    /**
109
     * Remove Asset on all Adapters
110
     *
111
     * @param Asset $asset
112
     */
113
    public function remove(Asset $asset)
114
    {
115
        $this->adapters->remove($asset);
116
    }
117
118
    /**
119
     *
120
     * @return array
121
     */
122
    public function getAccounts()
123
    {
124
        if (!$this->cache) {
125
            return [];
126
        }
127
128
        return $this->cache->contains($this->identifier) ? $this->cache->fetch($this->identifier) : [];
129
    }
130
131
    /**
132
     *
133
     * @param $vendor
134
     * @param $credentials
135
     */
136
    public function setAccount($vendor, $credentials)
137
    {
138
        if (!$this->cache) {
139
            return;
140
        }
141
142
        $accounts = $this->getAccounts();
143
        $accounts[$vendor] = $credentials;
144
145
        $this->cache->save($this->identifier, $accounts);
146
    }
147
148
    /**
149
     *
150
     * @param $identifier
151
     * @param $configurationPath
152
     * @param Cache $cache
153
     * @return Owner
154
     */
155
    public static function retrieveFromCache($identifier, $configurationPath, Cache $cache)
156
    {
157
        $owner = new self($identifier, $cache);
158
159
        if ($adapters = AdapterCollection::retrieveFromCache($owner, $configurationPath)) {
160
            $owner->setAdapters($adapters);
161
        }
162
163
        return $owner;
164
    }
165
}
166