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

Template::registerFiles()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 4
nop 0
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
     * @var Parameters
34
     */
35
    private $parameters;
36
37
    /**
38
     * @var string
39
     */
40
    private $section;
0 ignored issues
show
introduced by
The private property $section is not used, and could be removed.
Loading history...
41
42
    public function __construct(
43
        Parameters $parameters,
44
        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

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