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

Template::processHomeDir()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 16
nc 3
nop 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\Util\Filesystem;
20
use Dotfiles\Core\Util\Toolkit;
21
use Psr\Log\LoggerInterface;
22
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
23
use Symfony\Component\Finder\Finder;
24
25
class Template implements EventSubscriberInterface
26
{
27
    /**
28
     * @var Parameters
29
     */
30
    private $config;
31
32
    /**
33
     * @var LoggerInterface
34
     */
35
    private $logger;
36
37
    /**
38
     * @var string
39
     */
40
    private $section;
41
42
    public function __construct(
43
        Parameters $config,
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->config = $config;
48
        $this->logger = $logger;
49
    }
50
51
    public static function getSubscribedEvents()
52
    {
53
        return array(
54
            Constant::EVENT_RESTORE => array('onRestore', 255),
55
        );
56
    }
57
58
    public function onRestore(): void
59
    {
60
        $machine = getenv('DOTFILES_MACHINE_NAME');
61
        $config = $this->config;
62
        $sections = array('defaults', $machine);
63
64
        foreach ($sections as $name) {
65
            $this->section = $name;
66
            $this->debug("Processing <comment>$name</comment> section.");
67
            $backupDir = $config->get('dotfiles.backup_dir')."/src/{$name}";
68
            $this->processHomeDir($backupDir.'/home');
69
            $this->debug('');
70
            $this->debug('');
71
        }
72
    }
73
74
    private function debug($message, array $context = array()): void
75
    {
76
        $message = sprintf('[%s] %s', $this->section, $message);
77
        $this->logger->info($message, $context);
78
    }
79
80
    private function processHomeDir($homeDir): void
81
    {
82
        $targetDir = $this->config->get('dotfiles.home_dir');
83
        if (!is_dir($homeDir)) {
84
            $this->debug("Home directory <comment>$homeDir</comment> not found");
85
86
            return;
87
        }
88
89
        $files = Finder::create()
90
            ->in($homeDir)
91
            ->ignoreDotFiles(false)
92
            ->ignoreVCS(true)
93
            ->files()
94
        ;
95
96
        $fs = new Filesystem();
97
        /* @var \Symfony\Component\Finder\SplFileInfo $file */
98
        foreach ($files as $file) {
99
            $target = Toolkit::ensureDotPath($file->getRelativePathname());
100
            $targetFile = $targetDir.DIRECTORY_SEPARATOR.$target;
101
            Toolkit::ensureDir($targetDir);
102
            $fs->copy($file->getRealPath(), $targetFile);
103
            $this->debug('+restore: <comment>'.$target.'</comment>');
104
        }
105
    }
106
}
107