Completed
Pull Request — master (#132)
by
unknown
01:33
created

ResourceCollection   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 73
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A getContext() 0 4 1
A isTemporary() 0 4 1
A canBeProcessedInPlace() 0 17 5
1
<?php
2
3
/*
4
 * This file is part of Zippy.
5
 *
6
 * (c) Alchemy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Alchemy\Zippy\Resource;
13
14
use Alchemy\Zippy\Exception\InvalidArgumentException;
15
use Doctrine\Common\Collections\ArrayCollection;
16
17
class ResourceCollection extends ArrayCollection
18
{
19
    private $context;
20
    /**
21
     * @var bool
22
     */
23
    private $temporary;
24
25
    /**
26
     * Constructor
27
     * @param string $context
28
     * @param Resource[] $elements An array of Resource
29
     * @param bool $temporary
30
     */
31
    public function __construct($context, array $elements, $temporary)
32
    {
33
        array_walk($elements, function($element) {
34
            if (!$element instanceof Resource) {
35
                throw new InvalidArgumentException('ResourceCollection only accept Resource elements');
36
            }
37
        });
38
39
        $this->context = $context;
40
        $this->temporary = (bool) $temporary;
41
        parent::__construct($elements);
42
    }
43
44
    /**
45
     * Returns the context related to the collection
46
     *
47
     * @return string
48
     */
49
    public function getContext()
50
    {
51
        return $this->context;
52
    }
53
54
    /**
55
     * Tells whether the collection is temporary or not.
56
     *
57
     * A ResourceCollection is temporary when it required a temporary folder to
58
     * fetch data
59
     *
60
     * @return bool
61
     */
62
    public function isTemporary()
63
    {
64
        return $this->temporary;
65
    }
66
67
    /**
68
     * Returns true if all resources can be processed in place, false otherwise
69
     *
70
     * @return bool
71
     */
72
    public function canBeProcessedInPlace()
73
    {
74
        if (count($this) === 1) {
75
            if (null !== $context = $this->first()->getContextForProcessInSinglePlace()) {
76
                $this->context = $context;
77
                return true;
78
            }
79
        }
80
81
        foreach ($this as $resource) {
82
            if (!$resource->canBeProcessedInPlace($this->context)) {
83
                return false;
84
            }
85
        }
86
87
        return true;
88
    }
89
}
90