Completed
Pull Request — master (#9)
by ANTHONIUS
02:53
created

Hooks::debug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the dotfiles project.
7
 *
8
 *     (c) Anthonius Munthi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Dotfiles\Core\Processor;
15
16
use Dotfiles\Core\Constant;
17
use Dotfiles\Core\DI\Parameters;
18
use Dotfiles\Core\Event\Dispatcher;
19
use Psr\Log\LoggerInterface;
20
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
21
use Symfony\Component\Finder\Finder;
22
use Symfony\Component\Finder\SplFileInfo;
23
24
/**
25
 * Class Hooks.
26
 */
27
class Hooks implements EventSubscriberInterface
28
{
29
    /**
30
     * @var \Dotfiles\Core\DI\Parameters
31
     */
32
    private $config;
33
34
    /**
35
     * @var Dispatcher
36
     */
37
    private $dispatcher;
38
39
    /**
40
     * @var array
41
     */
42
    private $hooks;
43
44
    /**
45
     * @var LoggerInterface
46
     */
47
    private $logger;
48
49
    /**
50
     * @var ProcessRunner
51
     */
52
    private $runner;
53
54
    public function __construct(
55
        Parameters $config,
56
        Dispatcher $dispatcher,
57
        LoggerInterface $logger,
58
        ProcessRunner $runner
59
    ) {
60
        $this->config = $config;
61
        $this->dispatcher = $dispatcher;
62
        $this->logger = $logger;
63
        $this->runner = $runner;
64
    }
65
66
    public static function getSubscribedEvents()
67
    {
68
        return array(
69
            Constant::EVENT_PRE_RESTORE => 'onPreRestore',
70
            Constant::EVENT_POST_RESTORE => array('onPostRestore', -1),
71
        );
72
    }
73
74
    public function onPostRestore()
75
    {
76
        $this->doPostRestoreHooks();
77
    }
78
79
    public function onPreRestore()
80
    {
81
        $this->registerHooks();
82
        $this->doPreRestoreHooks();
83
    }
84
85
    private function debug($messages, $context = array()): void
86
    {
87
        $this->logger->info($messages, $context);
88
    }
89
90
    private function doPostRestoreHooks(): void
91
    {
92
        $hooks = $this->hooks['post']['restore'];
93
        $this->debug('Processing post-restore hooks');
94
        foreach ($hooks as $relPath => $realPath) {
95
            $this->doProcessHooks($relPath, $realPath);
96
        }
97
    }
98
99
    private function doPreRestoreHooks(): void
100
    {
101
        $hooks = $this->hooks['pre']['restore'];
102
        $this->debug('Start Processing pre-restore hooks');
103
        foreach ($hooks as $relPath => $realPath) {
104
            $this->doProcessHooks($relPath, $realPath);
105
        }
106
    }
107
108
    private function doProcessHooks($relPath, $realPath): void
0 ignored issues
show
Unused Code introduced by
The parameter $relPath is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

108
    private function doProcessHooks(/** @scrutinizer ignore-unused */ $relPath, $realPath): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
109
    {
110
        $runner = $this->runner;
111
        $runner->run($realPath);
112
    }
113
114
    private function registerHooks(): void
115
    {
116
        $this->hooks['pre']['restore'] = array();
117
        $this->hooks['post']['restore'] = array();
118
119
        $backupPath = $this->config->get('dotfiles.backup_dir').'/src';
120
        $machine = $this->config->get('dotfiles.machine_name');
121
        $dirs = array();
122
        if (is_dir($dir = $backupPath.'/defaults/hooks')) {
123
            $dirs[] = $dir;
124
        }
125
        if (is_dir($dir = $backupPath.'/'.$machine.'/hooks')) {
126
            $dirs[] = $dir;
127
        }
128
129
        if (!count($dirs) > 0) {
130
            $this->debug('+hooks no available hooks found');
131
132
            return;
133
        }
134
135
        $finder = Finder::create()
136
            ->in($backupPath)
137
            ->name('pre-*')
138
            ->name('post-*')
139
            ->path('defaults/hooks')
140
            ->path($machine.'/hooks')
141
        ;
142
143
        /* @var SplFileInfo $file */
144
        foreach ($finder->files() as $file) {
145
            $relPath = $file->getRelativePathname();
146
            $realPath = $file->getRealPath();
147
            $baseName = basename($file->getRealPath());
148
            if (false !== ($tlength = strpos($baseName, '.'))) {
149
                $baseName = substr($baseName, 0, $tlength);
150
            }
151
            $exp = explode('-', $baseName);
152
            if (!is_executable($realPath)) {
153
                $this->debug('-hooks not executable: '.$relPath);
154
155
                continue;
156
            }
157
            $type = $exp[0];
158
            $hookOn = $exp[1];
159
            $this->hooks[$type][$hookOn][$relPath] = $realPath;
160
            $this->debug('+hooks '.$relPath);
161
        }
162
    }
163
}
164