Completed
Push — master ( 88b8c9...f7af16 )
by Michael
02:28
created

FileBag::createSplFileInfoObject()   D

Complexity

Conditions 9
Paths 5

Size

Total Lines 26
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
rs 4.9091
cc 9
eloc 11
nc 5
nop 1
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 bool
0 ignored issues
show
Documentation introduced by
Should the return type not be \SplFileInfo|\SplFileInfo[]?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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