PollCommand::execute()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 55
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 55
ccs 0
cts 41
cp 0
rs 8.7752
cc 6
eloc 36
nc 5
nop 2
crap 42

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * PHPCI - Continuous Integration for PHP.
4
 *
5
 * @copyright    Copyright 2014, Block 8 Limited.
6
 * @license      https://github.com/Block8/PHPCI/blob/master/LICENSE.md
7
 *
8
 * @link         https://www.phptesting.org/
9
 */
10
11
namespace PHPCI\Command;
12
13
use b8\Store\Factory;
14
use b8\HttpClient;
15
use Monolog\Logger;
16
use PHPCI\Helper\Lang;
17
use Symfony\Component\Console\Command\Command;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\Yaml\Parser;
21
use PHPCI\Model\Build;
22
23
/**
24
 * Run console command - Poll github for latest commit id.
25
 *
26
 * @author       Jimmy Cleuren <[email protected]>
27
 */
28
class PollCommand extends Command
29
{
30
    /**
31
     * @var \Monolog\Logger
32
     */
33
    protected $logger;
34
35
    public function __construct(Logger $logger, $name = null)
36
    {
37
        parent::__construct($name);
38
        $this->logger = $logger;
39
    }
40
41
    protected function configure()
42
    {
43
        $this
44
            ->setName('phpci:poll-github')
45
            ->setDescription(Lang::get('poll_github'));
46
    }
47
48
    /**
49
     * Pulls all pending builds from the database and runs them.
50
     */
51
    protected function execute(InputInterface $input, OutputInterface $output)
52
    {
53
        $parser = new Parser();
54
        $yaml = file_get_contents(APPLICATION_PATH.'PHPCI/config.yml');
55
        $this->settings = $parser->parse($yaml);
0 ignored issues
show
Bug introduced by
The property settings does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
56
57
        $token = $this->settings['phpci']['github']['token'];
58
59
        if (!$token) {
60
            $this->logger->error(Lang::get('no_token'));
61
62
            return;
63
        }
64
65
        $buildStore = Factory::getStore('Build');
66
67
        $this->logger->addInfo(Lang::get('finding_projects'));
68
        $projectStore = Factory::getStore('Project');
69
        $result = $projectStore->getWhere();
70
        $this->logger->addInfo(Lang::get('found_n_projects', count($result['items'])));
71
72
        foreach ($result['items'] as $project) {
73
            $http = new HttpClient('https://api.github.com');
74
            $commits = $http->get('/repos/'.$project->getReference().'/commits', array('access_token' => $token));
75
76
            $last_commit = $commits['body'][0]['sha'];
77
            $last_committer = $commits['body'][0]['commit']['committer']['email'];
78
            $message = $commits['body'][0]['commit']['message'];
79
80
            $this->logger->info(Lang::get('last_commit_is', $project->getTitle(), $last_commit));
81
82
            if ($project->getLastCommit() != $last_commit && $last_commit != '') {
83
                $this->logger->info(
84
                    Lang::get('adding_new_build')
85
                );
86
87
                $build = new Build();
88
                $build->setProjectId($project->getId());
89
                $build->setCommitId($last_commit);
90
                $build->setStatus(Build::STATUS_NEW);
91
                $build->setBranch($project->getBranch());
92
                $build->setCreated(new \DateTime());
93
                $build->setCommitMessage($message);
94
                if (!empty($last_committer)) {
95
                    $build->setCommitterEmail($last_committer);
96
                }
97
                $buildStore->save($build);
98
99
                $project->setLastCommit($last_commit);
100
                $projectStore->save($project);
101
            }
102
        }
103
104
        $this->logger->addInfo(Lang::get('finished_processing_builds'));
105
    }
106
}
107