FileBag   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 0
loc 81
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getAllFileInfoObjects() 0 4 1
A emptyBag() 0 5 1
A initialize() 0 8 3
D createSplFileInfoObject() 0 26 9
1
<?php
2
namespace Michaels\Manager\Bags;
3
4
use Michaels\Manager\Exceptions\BadFileInfoObjectException;
5
6
/**
7
 * Class FileBag - a collection object needed to create SplFileInfo objects from file references, in order to inject
8
 * into the FileLoader. This collection class can only store SplFileInfo objects. Anything else will cause exceptions.
9
 *
10
 * @package Michaels\Manager\Bags
11
 */
12
class FileBag
13
{
14
    /**
15
     * @var array An array of SplFileInfo objects
16
     */
17
    protected $fileObjects = [];
18
19
    /**
20
     * Constructs a new FileBag
21
     * @param $arrayOfSplFileInfoObjects
22
     */
23
    public function __construct(array $arrayOfSplFileInfoObjects = [])
24
    {
25
        $this->initialize($arrayOfSplFileInfoObjects);
26
    }
27
28
    /**
29
     * Return an array of SplFileInfo objects
30
     * @return array
31
     */
32
    public function getAllFileInfoObjects()
33
    {
34
        return $this->fileObjects;
35
    }
36
37
    /**
38
     * Reset the FileBag back to an empty array.
39
     * @return array - an empty array
40
     */
41
    public function emptyBag()
42
    {
43
        $this->fileObjects = [];
44
        return $this->fileObjects;
45
    }
46
47
    /**
48
     * Set up the bag with a proper array of SplFileInfo objects
49
     * @param array $splFileInfoObjects
50
     * @internal param $arrayOfSplFileInfoObjects
51
     */
52
    protected function initialize(array $splFileInfoObjects = [])
53
    {
54
        if (!empty($splFileInfoObjects)) {
55
            foreach ($splFileInfoObjects as $object) {
56
                array_push($this->fileObjects, $this->createSplFileInfoObject($object));
57
            }
58
        }
59
    }
60
61
    /**
62
     * Check for an \SplFileInfo object or a custom namespaces object
63
     * @param $entity
64
     * @return \SplFileInfo|\SplFileInfo[]|string
65
     */
66
    protected function createSplFileInfoObject($entity)
67
    {
68
        // This is already a valid object
69
        if ($entity instanceof \SplFileInfo) {
70
            return $entity;
71
        }
72
73
        // This is a valid path
74
        if (is_string($entity) && file_exists($entity)) {
75
            return new \SplFileInfo($entity);
76
        }
77
78
        // This is an array with a valid object
79
        $isArray = is_array($entity);
80
        if ($isArray && $entity[0] instanceof \SplFileInfo) {
81
            return $entity;
82
        }
83
84
        // This is an array with a valid path
85
        if ($isArray && is_string($entity[0]) && file_exists($entity[0])) {
86
            return [new \SplFileInfo($entity[0]), $entity[1]];
87
        }
88
89
        // We've run out of options
90
        throw new BadFileInfoObjectException('The input array does not hold proper SplFileInfo objects.');
91
    }
92
}
93