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

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 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