Template   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 79
c 0
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A debug() 0 4 1
A run() 0 17 2
A processHomeDir() 0 22 3
A __construct() 0 8 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 Dotfiles\Core\Util\Filesystem;
19
use Dotfiles\Core\Util\Toolkit;
20
use Psr\Log\LoggerInterface;
21
use Symfony\Component\Finder\Finder;
22
23
class Template
24
{
25
    /**
26
     * @var Config
27
     */
28
    private $config;
29
30
    /**
31
     * @var Dispatcher
32
     */
33
    private $dispatcher;
34
35
    /**
36
     * @var LoggerInterface
37
     */
38
    private $logger;
39
40
    /**
41
     * @var string
42
     */
43
    private $section;
44
45
    public function __construct(
46
        Config $config,
47
        Dispatcher $dispatcher,
48
        LoggerInterface $logger
49
    ) {
50
        $this->config = $config;
51
        $this->logger = $logger;
52
        $this->dispatcher = $dispatcher;
53
    }
54
55
    public function run(): void
56
    {
57
        $machine = getenv('DOTFILES_MACHINE_NAME');
58
        $config = $this->config;
59
        $sections = array('defaults', $machine);
60
61
        foreach ($sections as $name) {
62
            $this->section = $name;
63
            $this->debug("Processing <comment>$name</comment> section.");
64
            $backupDir = $config->get('dotfiles.backup_dir')."/src/{$name}";
65
            $this->processHomeDir($backupDir.'/home');
66
            $this->debug('');
67
            $this->debug('');
68
        }
69
70
        $this->section = 'patch';
71
        $this->debug('applying patch');
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
            $fs->copy($file->getRealPath(), $targetDir.DIRECTORY_SEPARATOR.$target);
101
            $this->debug('+restore: <comment>'.$target.'</comment>');
102
        }
103
    }
104
}
105