GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ProjectVersionService::setStaticVersion()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace KamiYang\ProjectVersion\Service;
5
6
/*
7
 * This file is part of the ProjectVersion project.
8
 *
9
 * It is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * For the full copyright and license information, please read
15
 * LICENSE file that was distributed with this source code.
16
 */
17
18
use KamiYang\ProjectVersion\Configuration\ExtensionConfiguration;
19
use KamiYang\ProjectVersion\Enumeration\GitCommandEnumeration;
20
use KamiYang\ProjectVersion\Enumeration\ProjectVersionModeEnumeration;
21
use KamiYang\ProjectVersion\Facade\CommandUtilityFacade;
22
use KamiYang\ProjectVersion\Facade\SystemEnvironmentBuilderFacade;
23
use TYPO3\CMS\Core\SingletonInterface;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26
/**
27
 * Class ProjectVersionService
28
 */
29
class ProjectVersionService implements SingletonInterface
30
{
31
    /**
32
     * @var \KamiYang\ProjectVersion\Facade\SystemEnvironmentBuilderFacade
33
     */
34
    protected $systemEnvironmentBuilderFacade;
35
    /**
36
     * @var \KamiYang\ProjectVersion\Facade\CommandUtilityFacade
37
     */
38
    protected $commandUtilityFacade;
39
40
    /**
41
     * @api
42
     */
43
    public function getProjectVersion(): ProjectVersion
44
    {
45
        $projectVersion = GeneralUtility::makeInstance(ProjectVersion::class);
46
47
        switch (ExtensionConfiguration::getMode()) {
48
            case ProjectVersionModeEnumeration::STATIC_VERSION:
49
                $this->setStaticVersion($projectVersion);
50
                break;
51
            case ProjectVersionModeEnumeration::GIT:
52
                $this->setVersionFromGit($projectVersion);
53
                break;
54
            case ProjectVersionModeEnumeration::GIT_FILE_FALLBACK:
55
                $this->setVersionFromGit($projectVersion);
56
57
                if ($projectVersion->getVersion() === ProjectVersion::UNKNOWN_VERSION) {
58
                    //if version is still unknown, try to resolve version by file
59
                    $this->setVersionFromFile($projectVersion);
60
                }
61
                break;
62
            case ProjectVersionModeEnumeration::FILE:
63
            default:
64
                $this->setVersionFromFile($projectVersion);
65
        }
66
67
        return $projectVersion;
68
    }
69
70
    /**
71
     * @param $revision
72
     * @param $tag
73
     * @param $branch
74
     * @return string
75
     */
76
    private function formatVersionBasedOnConfiguration($revision, $tag, $branch): string
77
    {
78
        switch (ExtensionConfiguration::getGitFormat()) {
79
            case GitCommandEnumeration::FORMAT_REVISION:
80
                $format = $revision;
81
                break;
82
            case GitCommandEnumeration::FORMAT_REVISION_TAG:
83
                $format = \sprintf('[%s] %s', $revision, $tag);
84
                break;
85
            case GitCommandEnumeration::FORMAT_BRANCH:
86
                $format = $branch;
87
                break;
88
            case GitCommandEnumeration::FORMAT_TAG:
89
                $format = $tag;
90
                break;
91
            case GitCommandEnumeration::FORMAT_REVISION_BRANCH:
92
            default:
93
                $format = \sprintf('[%s] %s', $revision, $branch);
94
        }
95
96
        return $format;
97
    }
98
99
    /**
100
     * SystemEnvironmentBuilderFacade injector.
101
     *
102
     * @param \KamiYang\ProjectVersion\Facade\SystemEnvironmentBuilderFacade $systemEnvironmentBuilderFacade
103
     */
104
    public function injectSystemEnvironmentBuilderFacade(SystemEnvironmentBuilderFacade $systemEnvironmentBuilderFacade)
105
    {
106
        $this->systemEnvironmentBuilderFacade = $systemEnvironmentBuilderFacade;
107
    }
108
109
    /**
110
     * CommandUtilityFacade injector.
111
     *
112
     * @param \KamiYang\ProjectVersion\Facade\CommandUtilityFacade $commandUtilityFacade
113
     */
114
    public function injectCommandUtilityFacade(CommandUtilityFacade $commandUtilityFacade)
115
    {
116
        $this->commandUtilityFacade = $commandUtilityFacade;
117
    }
118
119
    /**
120
     * @return bool
121
     */
122
    protected function isGitAvailable(): bool
123
    {
124
        return $this->systemEnvironmentBuilderFacade->isFunctionDisabled('exec') === false &&
125
            // check if git exists
126
            $this->commandUtilityFacade->exec('git --version', $_, $returnCode) &&
127
            $returnCode === 0;
128
    }
129
130
    /**
131
     * @param \KamiYang\ProjectVersion\Service\ProjectVersion $projectVersion
132
     */
133
    private function setStaticVersion(ProjectVersion $projectVersion)
134
    {
135
        $projectVersion->setVersion(ExtensionConfiguration::getStaticVersion());
136
    }
137
138
    /**
139
     * Resolve version by common VERSION-file.
140
     *
141
     * @param \KamiYang\ProjectVersion\Service\ProjectVersion $projectVersion
142
     */
143
    private function setVersionFromFile(ProjectVersion $projectVersion)
144
    {
145
        $versionFilePath = ExtensionConfiguration::getAbsVersionFilePath();
146
        if (\file_exists($versionFilePath)) {
147
            $versionFileContent = \file_get_contents($versionFilePath);
148
            $projectVersion->setVersion($versionFileContent);
149
        }
150
    }
151
152
    /**
153
     * @param \KamiYang\ProjectVersion\Service\ProjectVersion $projectVersion
154
     */
155
    private function setVersionFromGit(ProjectVersion $projectVersion)
156
    {
157
        if ($this->isGitAvailable() === false) {
158
            return;
159
        }
160
161
        $version = $this->getVersionByFormat();
162
163
        if (!empty($version)) {
164
            /*
165
             * The icon identifier for "git" changed between TYPO3 v8 and v9.
166
             * For TYPO3 v8 it's "sysinfo-git" and for v9 it's "information-git"
167
             */
168
            $gitIconIdentifier = (float)TYPO3_version < 9 ? 'sysinfo-git' : 'information-git';
169
170
            $projectVersion->setVersion($version);
171
            $projectVersion->setIconIdentifier($gitIconIdentifier);
172
        }
173
    }
174
175
    /**
176
     * @return string
177
     */
178
    private function getVersionByFormat(): string
179
    {
180
        $branch = \trim($this->commandUtilityFacade->exec(GitCommandEnumeration::CMD_BRANCH));
181
        $revision = \trim($this->commandUtilityFacade->exec(GitCommandEnumeration::CMD_REVISION));
182
        $tag = \trim($this->commandUtilityFacade->exec(GitCommandEnumeration::CMD_TAG));
183
        $format = '';
184
185
        if ($branch || $revision || $tag) {
186
            $format = $this->formatVersionBasedOnConfiguration($revision, $tag, $branch);
187
        }
188
189
        return $format;
190
    }
191
}
192