StatusSendServiceImplementation   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
c 2
b 0
f 0
lcom 1
cbo 5
dl 0
loc 56
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B updateStatus() 0 27 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace PurpleBooth\GitGitHubLint;
5
6
use Github\Api\Repo;
7
use Github\Api\Repository\Statuses;
8
use Github\Client;
9
10
/**
11
 * Sets statuses on a SHA on GitHub
12
 *
13
 * @package PurpleBooth\GitGitHubLint
14
 */
15
class StatusSendServiceImplementation implements StatusSendService
16
{
17
    const CONTEXT               = 'Commit Message Style';
18
    const GITHUB_STATUS_SUCCESS = 'success';
19
    const GITHUB_STATUS_FAILURE = 'failure';
20
21
    /**
22
     * @var Client
23
     */
24
    private $client;
25
26
    /**
27
     * StatusSendServiceImplementation constructor.
28
     *
29
     * @param Client $client
30
     */
31
    public function __construct(Client $client)
32
    {
33
        $this->client = $client;
34
    }
35
36
    /**
37
     * Set a SHA in GitHub to a state
38
     *
39
     * @param string        $organisation
40
     * @param string        $repository
41
     * @param GitHubMessage $message
42
     */
43
    public function updateStatus(string $organisation, string $repository, GitHubMessage $message)
44
    {
45
        $status = $message->getHighestWeightStatus();
46
47
        /** @var Repo $repoApi */
48
        $repoApi = $this->client->repo();
49
50
        /** @var Statuses $statusesApi */
51
        $statusesApi = $repoApi->statuses();
52
        $state       = self::GITHUB_STATUS_SUCCESS;
53
54
        if (!$status->isPositive()) {
55
            $state = self::GITHUB_STATUS_FAILURE;
56
        }
57
58
        $statusesApi->create(
59
            $organisation,
60
            $repository,
61
            $message->getSha(),
62
            [
63
                'state'       => $state,
64
                'description' => $status->getMessage(),
65
                'context'     => self::CONTEXT,
66
                'target_url'  => $status->getDetailsUrl(),
67
            ]
68
        );
69
    }
70
}
71