• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

68publishers / tracy-git-version / 5982437370

26 Aug 2023 03:00AM UTC coverage: 90.041% (-2.2%) from 92.276%
5982437370

Pull #4

github

web-flow
Merge 60e13f581 into fa82db0dd
Pull Request #4: Feature/git executable

465 of 465 new or added lines in 32 files covered. (100.0%)

434 of 482 relevant lines covered (90.04%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.0
/src/Repository/ExportedGitRepository.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace SixtyEightPublishers\TracyGitVersion\Repository;
6

7
use JsonException;
8
use SixtyEightPublishers\TracyGitVersion\Repository\Command\GetHeadCommand;
9
use SixtyEightPublishers\TracyGitVersion\Repository\Command\GetLatestTagCommand;
10
use SixtyEightPublishers\TracyGitVersion\Repository\Export\CommandHandler\ExportedGitCommandHandlerInterface;
11
use SixtyEightPublishers\TracyGitVersion\Repository\Export\CommandHandler\GetHeadCommandHandler;
12
use SixtyEightPublishers\TracyGitVersion\Repository\Export\CommandHandler\GetLatestTagCommandHandler;
13
use function file_get_contents;
14
use function is_readable;
15
use function json_decode;
16

17
final class ExportedGitRepository extends AbstractGitRepository
18
{
19
    private string $file;
20

21
    private string $source;
22

23
    /** @var array<mixed, mixed>|null */
24
    private ?array $json = null;
25

26
    private ?bool $valid = null;
27

28
    /**
29
     * @param array<class-string, GitCommandHandlerInterface> $handlers
30
     */
31
    public function __construct(string $file, array $handlers = [], string $source = self::SOURCE_EXPORT)
32
    {
33
        $this->file = $file;
1✔
34
        $this->source = $source;
1✔
35

36
        parent::__construct($handlers);
1✔
37
    }
1✔
38

39
    public static function createDefault(string $file): self
40
    {
41
        return new self($file, [
×
42
            GetHeadCommand::class => new GetHeadCommandHandler(),
×
43
            GetLatestTagCommand::class => new GetLatestTagCommandHandler(),
×
44
        ]);
45
    }
46

47
    public function getSource(): string
48
    {
49
        return $this->source;
1✔
50
    }
51

52
    public function isAccessible(): bool
53
    {
54
        return $this->isValid();
1✔
55
    }
56

57
    public function addHandler(string $commandClassname, GitCommandHandlerInterface $handler): void
58
    {
59
        if ($handler instanceof ExportedGitCommandHandlerInterface && $this->isValid()) {
1✔
60
            $handler = $handler->withExportedValue($this->json ?? []);
1✔
61
        }
62

63
        parent::addHandler($commandClassname, $handler);
1✔
64
    }
1✔
65

66
    private function isValid(): bool
67
    {
68
        if (null !== $this->valid) {
1✔
69
            return $this->valid;
1✔
70
        }
71

72
        if (!is_readable($this->file)) {
1✔
73
            return $this->valid = false;
1✔
74
        }
75

76
        $content = @file_get_contents($this->file);
1✔
77

78
        if (false === $content) {
1✔
79
            return $this->valid = false;
×
80
        }
81

82
        try {
83
            $this->json = (array) json_decode($content, true, 512, JSON_THROW_ON_ERROR);
1✔
84

85
            return $this->valid = true;
1✔
86
        } catch (JsonException $e) {
×
87
            return $this->valid = false;
×
88
        }
89
    }
90
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc