ReactivateJob::execute()   C
last analyzed

Complexity

Conditions 7
Paths 226

Size

Total Lines 91
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 70
c 2
b 0
f 0
dl 0
loc 91
ccs 0
cts 38
cp 0
rs 6.7945
cc 7
nc 226
nop 0
crap 56

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
namespace App\Queue;
4
5
use App\Action\ActivateAction;
6
use App\Action\CleanupAction;
7
use App\Action\Context;
8
use App\Action\FinaliseAction;
9
use App\Builder;
10
use App\Facades\Log;
11
use App\Facades\Notifier;
12
use App\Facades\Provider;
13
use App\Facades\Settings;
14
use App\Model\Deployment;
15
use App\Model\Project;
16
use Exception;
17
use Ronanchilvers\Foundation\Config;
18
use Ronanchilvers\Foundation\Queue\Exception\FatalException;
19
use Ronanchilvers\Foundation\Queue\Job\Job;
20
use Ronanchilvers\Orm\Orm;
21
use Ronanchilvers\Utility\File;
22
use RuntimeException;
23
use Symfony\Component\Yaml\Yaml;
24
25
/**
26
 * Reactivate an existing deployment
27
 *
28
 * @author Ronan Chilvers <[email protected]>
29
 */
30
class ReactivateJob extends Job
31
{
32
    /**
33
     * @var string
34
     */
35
    protected $queue = 'deploy';
36
37
    /**
38
     * @var \App\Model\deployment
39
     */
40
    protected $original;
41
42
    /**
43
     * @var \App\Model\deployment
44
     */
45
    protected $deployment;
46
47
    /**
48
     * Class constructor
49
     *
50
     * @param \App\Model\Deployment $project
51
     * @author Ronan Chilvers <[email protected]>
52
     */
53
    public function __construct(Deployment $original, Deployment $deployment)
54
    {
55
        $this->original   = $original;
56
        $this->deployment = $deployment;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     *
62
     * @author Ronan Chilvers <[email protected]>
63
     */
64
    public function execute()
65
    {
66
        $project       = $this->deployment->project;
67
        $data          = Yaml::parse($this->deployment->configuration);
68
        $configuration = new Config($data);
69
        $builder       = new Builder(
70
            $project,
71
            $this->deployment
72
        );
73
        $baseDir    = Settings::get('build.base_dir');
74
        $key        = $project->key;
75
        $projectDir = File::join(
76
            $baseDir,
77
            $key
78
        );
79
        // We set the deployment_dir to the original one, not the new one!!
80
        $deploymentBaseDir = File::join(
81
            $projectDir,
82
            'deployments'
83
        );
84
        $deploymentDir = File::join(
85
            $deploymentBaseDir,
86
            $this->original->number
87
        );
88
        try {
89
            $context = new Context;
90
            $context->set('project_base_dir', $projectDir);
91
            $context->set('deployment_base_dir', $deploymentBaseDir);
92
            $context->set('deployment_dir', $deploymentDir);
93
            $builder->addAction(new ActivateAction);
94
            $builder->addAction(new FinaliseAction);
95
            $builder->addAction(new CleanupAction);
96
97
            if (!$this->deployment->start()) {
98
                throw new RuntimeException('Unable to mark the deployment as started');
99
            }
100
            $builder->run(
101
                $configuration,
102
                $context,
103
                function($data) use ($project) {
104
                    Log::debug($data, [
105
                        'project' => $project->toArray(),
106
                    ]);
107
                }
108
            );
109
            if (!$this->deployment->finish()) {
110
                throw new RuntimeException('Unable to mark the deployment as finished');
111
            }
112
            $provider = Provider::forProject($project);
113
            Notifier::send(
114
                sprintf(
115
                    "Reactivation completed for <%s|%s>\nSHA: <%s|%s>\nAuthor: %s",
116
                    $provider->getRepositoryLink($project->repository),
117
                    $project->repository,
118
                    $provider->getShaLink($project->repository, $this->deployment->sha),
119
                    $this->deployment->sha,
120
                    $this->deployment->author
121
                ),
122
                $configuration->get('notify', [])
123
            );
124
        } catch (Exception $ex) {
125
            Log::critical($ex->getMessage(), [
126
                'project'   => $project->toArray(),
127
                'deployment'   => $this->deployment->toArray(),
128
                'exception' => $ex,
129
            ]);
130
            if (!$this->deployment->fail()) {
131
                throw new RuntimeException('Unable to mark the deployment as failed');
132
            }
133
            $project->last_status = $this->deployment->status;
134
            if (!$project->save()) {
135
                throw new RuntimeException('Unable to project as failed');
136
            }
137
            Notifier::send(
138
                sprintf(
139
                    "Deployment failed for <%s|%s>\nSHA: <%s|%s>\nAuthor: %s",
140
                    $provider->getRepositoryLink($project->repository),
141
                    $project->repository,
142
                    $provider->getShaLink($project->repository, $this->deployment->sha),
143
                    $this->deployment->sha,
144
                    $this->deployment->author
145
                ),
146
                $configuration->get('notify', [])
147
            );
148
            throw new FatalException(
149
                $ex->getMessage(),
150
                $ex->getCode()
151
            );
152
        } finally {
153
            if (!$project->markActive()) {
154
                throw new RuntimeException('Unable to mark project as deploying');
155
            }
156
        }
157
    }
158
}
159