Completed
Push — master ( 2676ab...31a88b )
by Sergii
03:35
created

Exclude::setPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
nc 1
cc 1
eloc 2
nop 1
1
<?php
2
namespace AppBundle\Sync\Entity\Filter;
3
4
use AppBundle\Exception\ExcludeFilterException;
5
use AppBundle\Sync\Entity\File;
6
7
/**
8
 * Filter files by list of excluded elements
9
 *
10
 * @author Sergey Sadovoi <[email protected]>
11
 */
12
abstract class Exclude implements FilterInterface
13
{
14
    /**
15
     * @var string  Path to file with excludes
16
     */
17
    protected $path;
18
    /**
19
     * @var array  List of ids for exclude
20
     */
21
    protected $elements;
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    abstract public function valid(File $file);
27
28
    /**
29
     * Load the array of excluded items and save them to $elements
30
     *
31
     * @throws ExcludeFilterException
32
     */
33
    protected function load()
34
    {
35
        $result = @file_get_contents($this->getPath());
36
37
        if (false === $result) {
38
            throw new ExcludeFilterException(
39
                sprintf('[ExludeFilter] Error while loading file %s', $this->getPath()),
40
                ExcludeFilterException::MISSING_FILE
41
            );
42
        }
43
44
        $result = json_decode($result);
45
        if (is_null($result)) {
46
            throw new ExcludeFilterException(
47
                sprintf('[ExludeFilter] Error while loading exclude data from %s', $this->getPath()),
48
                ExcludeFilterException::INVALID_JSON
49
            );
50
        }
51
        if (!is_array($result)) {
52
            throw new ExcludeFilterException(
53
                sprintf('[ExludeFilter] Invalid result type "%s" for exclude data, must be an array', gettype($result)),
54
                ExcludeFilterException::NOT_ARRAY
55
            );
56
        }
57
58
        $this->elements = $result;
59
    }
60
61
    /**
62
     * Get the path to file with excludes
63
     *
64
     * @throws ExcludeFilterException
65
     *
66
     * @return string File path
67
     */
68
    public function getPath()
69
    {
70
        $path = $this->path;
71
        if ('' == $path) {
72
            throw new ExcludeFilterException(
73
                '[ExcludeFilter] Path must be defined',
74
                ExcludeFilterException::UNDEFINED_PATH
75
            );
76
        }
77
78
        return $this->path;
79
    }
80
81
    /**
82
     * Set the path to file with excludes
83
     *
84
     * @param string $path
85
     */
86
    public function setPath($path)
87
    {
88
        $this->path = $path;
89
    }
90
}
91