Filter   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 128
ccs 0
cts 30
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
run() 0 1 ?
A __construct() 0 9 1
A checkRequirements() 0 3 1
A clean() 0 8 2
A getCacheDir() 0 11 2
A getName() 0 4 1
A findFilesInCacheDir() 0 9 1
A addNotification() 0 10 1
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
     *
41
     * @return SplFileInfo[]
42
     */
43
    abstract public function run(array $files, array $destinations);
44
    
45
    /**
46
     * Filter constructor.
47
     *
48
     * @param string                   $name
49
     * @param ConfigurationInterface   $configuration
50
     * @param EventDispatcherInterface $eventDispatcher
51
     */
52
    public function __construct(
53
        $name,
54
        ConfigurationInterface $configuration,
55
        EventDispatcherInterface $eventDispatcher
56
    ) {
57
        $this->name = $name;
58
        $this->configuration = $configuration;
59
        $this->eventDispatcher = $eventDispatcher;
60
    }
61
    
62
    /**
63
     * Use this method to check your filter requirements (for example, if the library required by this filter are
64
     * installed).
65
     */
66
    public function checkRequirements()
67
    {
68
    }
69
    
70
    /**
71
     * Remove the assets cache directory.
72
     */
73
    public function clean()
74
    {
75
        $fileSystem = new Filesystem();
76
        
77
        if ($fileSystem->exists($this->getCacheDir())) {
78
            $fileSystem->remove($this->getCacheDir());
79
        }
80
    }
81
    
82
    /**
83
     * Return the assets cache dir.
84
     *
85
     * @return string
86
     */
87
    public function getCacheDir()
88
    {
89
        $fileSystem = new Filesystem();
90
        $path = 'var/cache/assets/'.$this->name.'/';
91
        
92
        if (!$fileSystem->exists($path)) {
93
            $fileSystem->mkdir($path);
94
        }
95
        
96
        return $path;
97
    }
98
    
99
    /**
100
     * @return string
101
     */
102
    public function getName()
103
    {
104
        return $this->name;
105
    }
106
    
107
    /**
108
     * Return files matching $pattern in filters cache directory.
109
     *
110
     * @param string $pattern
111
     *
112
     * @return Finder|SplFileInfo[]
113
     */
114
    protected function findFilesInCacheDir($pattern)
115
    {
116
        $finder = new Finder();
117
        
118
        return $finder
119
            ->name($pattern)
120
            ->in($this->getCacheDir())
121
        ;
122
    }
123
    
124
    /**
125
     * Add a notification to the subscriber.
126
     *
127
     * @param $message
128
     */
129
    protected function addNotification($message)
130
    {
131
        $event = new NotificationEvent();
132
        $event->setMessage($message);
133
        
134
        $this
135
            ->eventDispatcher
136
            ->dispatch(NotificationEvent::NAME, $event)
137
        ;
138
    }
139
}
140