AdapterCollection   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 14
c 3
b 1
f 1
lcom 0
cbo 4
dl 0
loc 98
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetSet() 0 8 2
A upload() 0 6 2
A update() 0 6 2
A remove() 0 6 2
A retrieveFromCache() 0 14 3
A buildForAsset() 0 17 3
1
<?php
2
3
namespace Libcast\AssetDistributor\Adapter;
4
5
use Libcast\AssetDistributor\Asset\Asset;
6
use Libcast\AssetDistributor\Configuration\ConfigurationFactory;
7
use Libcast\AssetDistributor\Owner;
8
9
class AdapterCollection extends \ArrayIterator
10
{
11
    /**
12
     *
13
     * @param string $key
14
     * @param Adapter $adapter
15
     * @throws \Exception
16
     */
17
    public function offsetSet($key, $adapter)
18
    {
19
        if (!$adapter instanceof Adapter) {
20
            throw new \Exception('AdapterCollection can only contain Adapter objects');
21
        }
22
23
        parent::offsetSet($key, $adapter);
24
    }
25
26
    /**
27
     *
28
     * @param Asset $asset
29
     */
30
    public function upload(Asset $asset)
31
    {
32
        foreach ($this as $adapter) { /** @var Adapter $adapter */
33
            $adapter->upload($asset);
34
        }
35
    }
36
37
    /**
38
     *
39
     * @param Asset $asset
40
     */
41
    public function update(Asset $asset)
42
    {
43
        foreach ($this as $adapter) { /** @var Adapter $adapter */
44
            $adapter->update($asset);
45
        }
46
    }
47
48
    /**
49
     *
50
     * @param Asset $asset
51
     */
52
    public function remove(Asset $asset)
53
    {
54
        foreach ($this as $adapter) { /** @var Adapter $adapter */
55
            $adapter->remove($asset);
56
        }
57
    }
58
59
    /**
60
     *
61
     * @param Owner  $owner
62
     * @param string $configurationPath
63
     * @return AdapterCollection
64
     * @throws \Exception
65
     */
66
    public static function retrieveFromCache(Owner $owner, $configurationPath)
67
    {
68
        $collection = new self;
69
70
        if (!$accounts = $owner->getAccounts()) {
71
            return $collection;
72
        }
73
74
        foreach ($accounts as $vendor => $credentials) {
75
            $collection[] = AdapterFactory::build($vendor, $owner, $configurationPath);
76
        }
77
78
        return $collection;
79
    }
80
81
    /**
82
     *
83
     * @param Asset  $asset
84
     * @param Owner  $owner
85
     * @param string $configurationPath
86
     * @return AdapterCollection
87
     * @throws \Exception
88
     */
89
    public static function buildForAsset(Asset $asset, Owner $owner, $configurationPath)
90
    {
91
        $collection = new self;
92
93
        $vendors = ConfigurationFactory::getVendors($configurationPath);
94
        foreach ($vendors as $vendor) {
95
            $class = AdapterFactory::getClassName($vendor); /** @var Adapter $class */
96
97
            if (!$class::support($asset)) {
98
                continue;
99
            }
100
101
            $collection[] = new $class($owner, $configurationPath);
102
        }
103
104
        return $collection;
105
    }
106
}
107