Passed
Pull Request — master (#6)
by ANTHONIUS
02:38
created

Hooks   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
dl 0
loc 111
c 0
b 0
f 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
B registerHooks() 0 33 4
A doPostRestoreHooks() 0 6 2
A doProcessHooks() 0 13 1
A __construct() 0 8 1
A doPreRestoreHooks() 0 6 2
A run() 0 6 1
A debug() 0 3 1
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\Config\Config;
17
use Dotfiles\Core\Event\Dispatcher;
18
use Psr\Log\LoggerInterface;
19
use Symfony\Component\Console\Helper\DebugFormatterHelper;
20
use Symfony\Component\Finder\Finder;
21
use Symfony\Component\Process\Process;
22
23
class Hooks
24
{
25
    /**
26
     * @var Config
27
     */
28
    private $config;
29
30
    /**
31
     * @var Dispatcher
32
     */
33
    private $dispatcher;
34
    /**
35
     * @var array
36
     */
37
    private $hooks;
38
39
    /**
40
     * @var LoggerInterface
41
     */
42
    private $logger;
43
44
    public function __construct(
45
        Config $config,
46
        Dispatcher $dispatcher,
47
        LoggerInterface $logger
48
    ) {
49
        $this->config = $config;
50
        $this->dispatcher = $dispatcher;
51
        $this->logger = $logger;
52
    }
53
54
    public function run(): void
55
    {
56
        $this->hooks = array();
57
        $this->registerHooks();
58
        $this->doPreRestoreHooks();
59
        $this->doPostRestoreHooks();
60
    }
61
62
    private function debug($messages, $context = array()): void
63
    {
64
        $this->logger->info($messages, $context);
65
    }
66
67
    private function doPostRestoreHooks(): void
68
    {
69
        $hooks = $this->hooks['post']['restore'];
70
        $this->debug('Processing post-restore hooks');
71
        foreach ($hooks as $relPath => $realPath) {
72
            $this->doProcessHooks($relPath, $realPath);
73
        }
74
    }
75
76
    private function doPreRestoreHooks(): void
77
    {
78
        $hooks = $this->hooks['pre']['restore'];
79
        $this->debug('Start Processing pre-restore hooks');
80
        foreach ($hooks as $relPath => $realPath) {
81
            $this->doProcessHooks($relPath, $realPath);
82
        }
83
    }
84
85
    private function doProcessHooks($relPath, $realPath): void
86
    {
87
        $helper = new DebugFormatterHelper();
88
        $logger = $this->logger;
89
        $logger->debug("Executing <comment>$relPath</comment>");
90
        $process = new Process($realPath);
91
        $process->run(function ($type, $buffer) use ($relPath,$logger,$helper,$process): void {
0 ignored issues
show
Unused Code introduced by
The import $relPath is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
92
            $contents = $helper->start(
93
                spl_object_hash($process),
94
                $buffer,
95
                Process::ERR === $type
96
            );
97
            $logger->debug('OUTPUT >>'.$contents);
98
        });
99
    }
100
101
    private function registerHooks(): void
102
    {
103
        $this->hooks['pre']['restore'] = array();
104
        $this->hooks['post']['restore'] = array();
105
106
        $backupPath = $this->config->get('dotfiles.backup_dir');
107
        $finder = Finder::create()
108
            ->in($backupPath.'/src')
109
            ->path('hooks')
110
            ->name('pre-*')
111
            ->name('post-*')
112
        ;
113
114
        /* @var \SplFileInfo $file */
115
        foreach ($finder->files() as $file) {
116
            $relPath = $file->getRelativePathname();
117
            $realPath = $file->getRealPath();
118
119
            $baseName = basename($file->getRealPath());
120
            if (false !== ($tlength = strpos($baseName, '.'))) {
121
                $baseName = substr($baseName, 0, $tlength);
122
            }
123
            $exp = explode('-', $baseName);
124
125
            if (!is_executable($realPath)) {
126
                $this->debug('-hooks not executable: '.$relPath);
127
128
                continue;
129
            }
130
            $type = $exp[0];
131
            $hookOn = $exp[1];
132
            $this->hooks[$type][$hookOn][$relPath] = $realPath;
133
            $this->debug('+hooks '.$relPath);
134
        }
135
    }
136
}
137