Passed
Pull Request — master (#9)
by ANTHONIUS
03:11
created

Template   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
dl 0
loc 86
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A findBackupFiles() 0 14 2
A onPreRestore() 0 4 1
A registerFiles() 0 16 3
A restore() 0 7 2
A getSubscribedEvents() 0 5 1
A onRestore() 0 3 1
A __construct() 0 7 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\Constant;
17
use Dotfiles\Core\DI\Parameters;
18
use Dotfiles\Core\Event\Dispatcher;
19
use Dotfiles\Core\Event\RestoreEvent;
20
use Dotfiles\Core\Util\Filesystem;
21
use Dotfiles\Core\Util\Toolkit;
22
use Psr\Log\LoggerInterface;
23
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
24
use Symfony\Component\Finder\Finder;
25
26
class Template implements EventSubscriberInterface
27
{
28
    /**
29
     * @var LoggerInterface
30
     */
31
    private $logger;
32
33
    /**
34
     * @var Parameters
35
     */
36
    private $parameters;
37
38
    /**
39
     * @var string
40
     */
41
    private $section;
0 ignored issues
show
introduced by
The private property $section is not used, and could be removed.
Loading history...
42
43
    public function __construct(
44
        Parameters $parameters,
45
        Dispatcher $dispatcher,
0 ignored issues
show
Unused Code introduced by
The parameter $dispatcher 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

45
        /** @scrutinizer ignore-unused */ Dispatcher $dispatcher,

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...
46
        LoggerInterface $logger
47
    ) {
48
        $this->parameters = $parameters;
49
        $this->logger = $logger;
50
    }
51
52
    public static function getSubscribedEvents()
53
    {
54
        return array(
55
            Constant::EVENT_PRE_RESTORE => array('onPreRestore', 255),
56
            Constant::EVENT_RESTORE => array('onRestore', 255),
57
        );
58
    }
59
60
    public function onPreRestore(RestoreEvent $event)
61
    {
62
        $files = $this->registerFiles();
63
        $event->setFiles($files);
64
    }
65
66
    public function onRestore(RestoreEvent $event): void
67
    {
68
        $this->restore($event->getFiles());
69
    }
70
71
    private function findBackupFiles($dir)
72
    {
73
        $finder = Finder::create()
74
            ->ignoreVCS(true)
75
            ->ignoreDotFiles(false)
76
            ->in($dir)
77
        ;
78
        $files = array();
79
        foreach ($finder->files() as $file) {
80
            $dotFile = Toolkit::ensureDotPath($file->getRelativePathName());
81
            $files[$dotFile] = $file->getRealpath();
82
        }
83
84
        return $files;
85
    }
86
87
    private function registerFiles()
88
    {
89
        $params = $this->parameters;
90
        $sources = $params->get('dotfiles.backup_dir').'/src';
91
        $machineName = $params->get('dotfiles.machine_name');
92
93
        $defaults = $machine = array();
94
        if (is_dir($dir = $sources.'/defaults/home')) {
95
            $defaults = $this->findBackupFiles($dir);
96
        }
97
        if (is_dir($dir = $sources.'/'.$machineName.'/home')) {
98
            $machine = $this->findBackupFiles($dir);
99
        }
100
        $files = array_merge($defaults, $machine);
101
102
        return $files;
103
    }
104
105
    private function restore($files)
106
    {
107
        $homeDir = $this->parameters->get('dotfiles.home_dir');
108
        $fs = new Filesystem();
109
        foreach ($files as $relativePath => $source) {
110
            $target = $homeDir.DIRECTORY_SEPARATOR.$relativePath;
111
            $fs->copy($source, $target);
112
        }
113
    }
114
}
115