Completed
Pull Request — master (#1)
by Arnaud
58:13 queued 48:12
created

Filter::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace JK\Sam\Filter;
4
5
use JK\Sam\Configuration\ConfigurationInterface;
6
use JK\Sam\Event\NotificationEvent;
7
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
8
use Symfony\Component\Filesystem\Filesystem;
9
use Symfony\Component\Finder\Finder;
10
use SplFileInfo;
11
12
abstract class Filter implements FilterInterface
13
{
14
    /**
15
     * Filter's name
16
     * 
17
     * @var string
18
     */
19
    protected $name;
20
21
    /**
22
     * Filter's configuration
23
     * 
24
     * @var ConfigurationInterface
25
     */
26
    protected $configuration;
27
    
28
    /**
29
     * Notification dispatcher
30
     * 
31
     * @var EventDispatcherInterface
32
     */
33
    protected $eventDispatcher;
34
35
    /**
36
     * Apply a filter to a list of source files
37
     *
38
     * @param SplFileInfo[] $files
39
     * @param SplFileInfo[] $destinations
40
     * @return SplFileInfo[]
41
     */
42
    abstract public function run(array $files, array $destinations);
43
44
    /**
45
     * Filter constructor.
46
     *
47
     * @param string $name
48
     * @param ConfigurationInterface $configuration
49
     * @param EventDispatcherInterface $eventDispatcher
50
     */
51
    public function __construct(
52
        $name,
53
        ConfigurationInterface $configuration,
54
        EventDispatcherInterface $eventDispatcher
55
    ) {
56
        $this->name = $name;
57
        $this->configuration = $configuration;
58
        $this->eventDispatcher = $eventDispatcher;
59
    }
60
61
    /**
62
     * Use this method to check your filter requirements (for example, if the library required by this filter are
63
     * installed).
64
     */
65
    public function checkRequirements()
66
    {        
67
    }
68
69
    /**
70
     * Remove the assets cache directory.
71
     */
72
    public function clean()
73
    {
74
        $fileSystem = new Filesystem();
75
76
        if ($fileSystem->exists($this->getCacheDir())) {
77
            $fileSystem->remove($this->getCacheDir());
78
        }
79
    }
80
81
    /**
82
     * Return the assets cache dir.
83
     *
84
     * @return string
85
     */
86
    public function getCacheDir()
87
    {
88
        $fileSystem = new Filesystem();
89
        $path = 'var/cache/assets/'.$this->name.'/';
90
91
        if (!$fileSystem->exists($path)) {
92
            $fileSystem->mkdir($path);
93
        }
94
95
        return $path;
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    public function getName()
102
    {
103
        return $this->name;
104
    }
105
106
    /**
107
     * Return files matching $pattern in filters cache directory.
108
     *
109
     * @param string $pattern
110
     * @return Finder|SplFileInfo[]
111
     */
112
    protected function findFilesInCacheDir($pattern)
113
    {
114
        $finder = new Finder();
115
116
        return $finder
117
            ->name($pattern)
118
            ->in($this->getCacheDir());
119
    }
120
121
    /**
122
     * Add a notification to the subscriber.
123
     *
124
     * @param $message
125
     */
126
    protected function addNotification($message)
127
    {
128
        $event = new NotificationEvent();
129
        $event->setMessage($message);
130
131
        $this
132
            ->eventDispatcher
133
            ->dispatch(NotificationEvent::NAME, $event);
134
    }
135
}
136