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.
Completed
Push — master ( 1882f2...e320a1 )
by Loick
02:15
created

BitbucketUrlGenerator::generateCompareUrl()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 2

Importance

Changes 5
Bugs 0 Features 4
Metric Value
c 5
b 0
f 4
dl 0
loc 29
ccs 22
cts 22
cp 1
rs 8.8571
cc 2
eloc 20
nc 2
nop 4
crap 2
1
<?php
2
3
/*
4
 * This file is part of the composer-changelogs project.
5
 *
6
 * (c) Loïck Piera <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Pyrech\ComposerChangelogs\UrlGenerator;
13
14
use Pyrech\ComposerChangelogs\Version;
15
16
class BitbucketUrlGenerator extends AbstractUrlGenerator
17
{
18
    const DOMAIN = 'bitbucket.org';
19
    const URL_REGEX = '@bitbucket.org/(?P<user>[^/]+)/(?P<repository>[^/]+)@';
20
    const SSH_REGEX = '/^git@bitbucket\.org:(?P<user>[^\/]+)\/(?P<repository>.+)\.git$/';
21
22
    /**
23
     * {@inheritdoc}
24
     */
25 4
    public function supports($sourceUrl)
26
    {
27 4
        return strpos($sourceUrl, self::DOMAIN) !== false;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 10
    public function generateCompareUrl($sourceUrlFrom, Version $versionFrom, $sourceUrlTo, Version $versionTo)
34
    {
35 10
        $sourceUrlFrom = $this->generateBaseUrl($this->reformatSshUrl($sourceUrlFrom));
36 10
        $sourceUrlTo = $this->generateBaseUrl($this->reformatSshUrl($sourceUrlTo));
37
38
        // Check if comparison across forks is needed
39 10
        if ($sourceUrlFrom !== $sourceUrlTo) {
40 4
            $repositoryFrom = $this->extractRepositoryInformation($sourceUrlFrom);
41 4
            $repositoryTo = $this->extractRepositoryInformation($sourceUrlTo);
42
43 2
            return sprintf(
44 2
                '%s/branches/compare/%s/%s:%s%%0D%s/%s:%s',
45 2
                $sourceUrlTo,
46 2
                $repositoryTo['user'],
47 2
                $repositoryTo['repository'],
48 2
                $this->getCompareVersion($versionTo),
49 2
                $repositoryFrom['user'],
50 2
                $repositoryFrom['repository'],
51 2
                $this->getCompareVersion($versionFrom)
52 2
            );
53
        }
54
55 6
        return sprintf(
56 6
            '%s/branches/compare/%s%%0D%s',
57 6
            $sourceUrlTo,
58 6
            $this->getCompareVersion($versionTo),
59 6
            $this->getCompareVersion($versionFrom)
60 6
        );
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 2
    public function generateReleaseUrl($sourceUrl, Version $version)
67
    {
68
        // Releases are not supported on Bitbucket :'(
69 2
        return false;
70
    }
71
72
    /**
73
     * @param string $sourceUrl
74
     *
75
     * @return array
76
     */
77 4
    private function extractRepositoryInformation($sourceUrl)
78
    {
79 4
        preg_match(self::URL_REGEX, $sourceUrl, $matches);
80
81 4
        if (!isset($matches['user']) || !isset($matches['repository'])) {
82 2
            throw new \LogicException(
83 2
                sprintf('Malformed Bitbucket source url: "%s"', $sourceUrl)
84 2
            );
85
        }
86
87
        return [
88 4
            'user' => $matches['user'],
89 4
            'repository' => $matches['repository'],
90 4
        ];
91
    }
92
93
    /**
94
     * @param string $url
95
     *
96
     * @return string
97
     */
98 10
    private function reformatSshUrl($url)
99
    {
100 10
        if (preg_match(self::SSH_REGEX, $url, $matches)) {
101 2
            return sprintf(
102 2
                'https://bitbucket.org/%s/%s',
103 2
                $matches['user'],
104 2
                $matches['repository']
105 2
            );
106
        }
107
108 8
        return $url;
109
    }
110
}
111