Completed
Push — master ( 255478...d0abf8 )
by Michael
02:52
created

LoadsFilesTrait::loadFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
namespace Michaels\Manager\Traits;
3
4
/**
5
 * Allows a class to have file loading capabilities.
6
 *
7
 * @implements Michaels\Manager\Contracts\ManagesItemsInterface
8
 * @package Michaels\Manager
9
 */
10
11
use Michaels\Manager\Contracts\DecoderInterface;
12
use Michaels\Manager\FileLoader;
13
14
/**
15
 * Loads data from configuration-type files into Manager
16
 * @package Michaels\Manager\Traits
17
 */
18
trait LoadsFilesTrait
19
{
20
    use DependsOnManagesItemsTrait;
21
22
    /** @var  \Michaels\Manager\FileLoader Instance of the FileLoader */
23
    protected $fileLoader;
24
25
    /**
26
     * Makes sure a FileLoader object was created or creates one.
27
     * @param FileLoader $fileLoader
28
     */
29
    protected function initializeFileLoader(FileLoader $fileLoader = null)
30
    {
31
        if (!isset($this->fileLoader)) {
32
            $this->setFileLoader(($fileLoader) ? $fileLoader : new FileLoader());
33
        }
34
    }
35
36
    /**
37
     * This method adds the file loading functionality.
38
     *
39
     * @param array $files an array of SplFileInfo Objects
40
     * @param $append boolean true, if data should be appended to the manager.
41
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null?

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...
42
     */
43
    public function loadFiles(array $files, $append = false)
44
    {
45
        $this->initializeFileLoader();
46
        $this->fileLoader->addFiles($files);
47
        $data = $this->fileLoader->process();
48
        $this->hydrate($data, $append);
0 ignored issues
show
Bug introduced by
It seems like hydrate() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
49
    }
50
51
    public function loadFile($file, $append = false)
52
    {
53
        return $this->loadFiles([$file], $append);
54
    }
55
56
    /**
57
     * Allows for the addition of a custom decoder to the manager load files system.
58
     *
59
     * @param DecoderInterface $decoder
60
     * @return mixed
61
     */
62
    public function addDecoder(DecoderInterface $decoder)
63
    {
64
        $this->initializeFileLoader();
65
        $this->fileLoader->addDecoder($decoder);
66
    }
67
68
    /**
69
     * @return FileLoader
70
     */
71
    public function getFileLoader()
72
    {
73
        return $this->fileLoader;
74
    }
75
76
    /**
77
     * @param FileLoader $fileLoader
78
     */
79
    public function setFileLoader($fileLoader)
80
    {
81
        $this->fileLoader = $fileLoader;
82
    }
83
}
84