Completed
Branch guard_coverage (f2aaf0)
by Pablo
03:07
created

PhpGuardCoverageGitIgnoreConfigurator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 5
c 1
b 1
f 0
lcom 1
cbo 5
dl 0
loc 56
ccs 14
cts 14
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A configure() 0 11 2
A isFileIgnored() 0 4 1
A getContent() 0 4 1
1
<?php
2
3
namespace PhpGitHooks\Module\Configuration\Service;
4
5
use PhpGitHooks\Infrastructure\CommandBus\CommandBus\CommandBus;
6
use PhpGitHooks\Infrastructure\CommandBus\QueryBus\QueryBus;
7
use PhpGitHooks\Module\Git\Contract\Command\GitIgnoreWriterCommand;
8
use PhpGitHooks\Module\Git\Contract\Query\GitIgnoreExtractorQuery;
9
use PhpGitHooks\Module\Git\Contract\Response\GitIgnoreDataResponse;
10
11
class PhpGuardCoverageGitIgnoreConfigurator
12
{
13
    const FILE_TO_IGNORE = '.guard_coverage';
14
    /**
15
     * @var QueryBus
16
     */
17
    private $queryBus;
18
    /**
19
     * @var CommandBus
20
     */
21
    private $commandBus;
22
23
    /**
24
     * PhpGuardCoverageGitIgnoreConfigurator constructor.
25
     *
26
     * @param QueryBus   $queryBus
27
     * @param CommandBus $commandBus
28
     */
29 5
    public function __construct(QueryBus $queryBus, CommandBus $commandBus)
30
    {
31 5
        $this->queryBus = $queryBus;
32 5
        $this->commandBus = $commandBus;
33 5
    }
34
35 3
    public function configure()
36
    {
37
        /** @var GitIgnoreDataResponse $gitIgnoreContent */
38 3
        $gitIgnoreContent = $this->queryBus->handle(new GitIgnoreExtractorQuery());
39
40 3
        if (false === $this->isFileIgnored($gitIgnoreContent->getContent())) {
41 2
            $content = $this->getContent($gitIgnoreContent->getContent());
42
43 2
            $this->commandBus->handle(new GitIgnoreWriterCommand($content));
44
        }
45 3
    }
46
47
    /**
48
     * @param $data
49
     *
50
     * @return bool
51
     */
52 3
    private function isFileIgnored($data)
53
    {
54 3
        return false !== strpos($data, static::FILE_TO_IGNORE);
55
    }
56
57
    /**
58
     * @param string $gitIgnoreContent
59
     *
60
     * @return string
61
     */
62 2
    private function getContent($gitIgnoreContent)
63
    {
64 2
        return trim($gitIgnoreContent)."\n#Entry generated by php-git-hooks tool.\n".self::FILE_TO_IGNORE;
65
    }
66
}
67