PublishUpdateAssignment::handle()   B
last analyzed

Complexity

Conditions 7
Paths 64

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 9
nc 64
nop 0
dl 0
loc 13
rs 8.2222
c 0
b 0
f 0
1
<?php
2
3
namespace Acacha\ForgePublish\Commands;
4
5
use Acacha\ForgePublish\Commands\Traits\AborstIfEnvVariableIsnotInstalled;
6
use Acacha\ForgePublish\Commands\Traits\InteractsWithAssignments;
7
use Acacha\ForgePublish\Commands\Traits\InteractsWithEnvironment;
8
use Acacha\ForgePublish\Commands\Traits\InteractsWithLocalGithub;
9
use Acacha\ForgePublish\Commands\Traits\ItFetchesAssignments;
10
use GuzzleHttp\Client;
11
use Illuminate\Console\Command;
12
13
/**
14
 * Class PublishUpdateAssignment.
15
 *
16
 * @package Acacha\ForgePublish\Commands
17
 */
18
class PublishUpdateAssignment extends Command
19
{
20
    use InteractsWithEnvironment, AborstIfEnvVariableIsnotInstalled, InteractsWithAssignments, ItFetchesAssignments;
21
22
    /**
23
     * The name and signature of the console command.
24
     *
25
     * @var string
26
     */
27
    protected $signature = 'publish:update_assignment {assignment? : The assignment to update}
28
                                                        {name? : The name description}
29
                                                        {repository_uri? : The repository URI}
30
                                                        {repository_type? : The repository type}
31
                                                        {forge_site? : The Laravel Forge site id}
32
                                                        {forge_server? : The Laravel Forge Server}';
33
34
    /**
35
     * The console command description.
36
     *
37
     * @var string
38
     */
39
    protected $description = 'Updates an assignment';
40
41
    /**
42
     * Server names.
43
     *
44
     * @var Client
45
     */
46
    protected $http;
47
48
    /**
49
     * Assignments.
50
     *
51
     * @var []
52
     */
53
    protected $assignments;
54
55
    /**
56
     * Assignment.
57
     *
58
     * @var integer
59
     */
60
    protected $assignment;
61
62
    /**
63
     * Assignment name
64
     *
65
     * @var String
66
     */
67
    protected $assignmentName;
68
69
    /**
70
     * Repository uri.
71
     *
72
     * @var String
73
     */
74
    protected $repository_uri;
75
76
    /**
77
     * Repository type.
78
     *
79
     * @var String
80
     */
81
    protected $repository_type;
82
83
    /**
84
     * Laravel Forge site id.
85
     *
86
     * @var String
87
     */
88
    protected $forge_site;
89
90
    /**
91
     * Laravel Forge server id.
92
     *
93
     * @var String
94
     */
95
    protected $forge_server;
96
97
    /**
98
     * SaveEnvVariable constructor.
99
     *
100
     */
101
    public function __construct(Client $http)
102
    {
103
        parent::__construct();
104
        $this->http = $http;
105
    }
106
107
    /**
108
     * Execute the console command.
109
     *
110
     */
111
    public function handle()
112
    {
113
        $this->abortCommandExecution();
114
115
        $this->assignment = $this->argument('assignment') ? $this->argument('assignment') : $this->askAssignment();
116
        $this->assignmentName = $this->argument('name') ? $this->argument('name') : $this->askName();
117
        $this->repository_uri = $this->argument('repository_uri') ? $this->argument('repository_uri') : $this->askRepositoryUri();
118
        $this->repository_type = $this->argument('repository_type') ? $this->argument('repository_type') : $this->askRepositoryType();
119
        $this->forge_site = $this->argument('forge_site') ? $this->argument('forge_site') : $this->askForgeSite();
120
        $this->forge_server = $this->argument('forge_server') ? $this->argument('forge_server') : $this->askForgeServer();
121
122
        $this->updateAssignment();
123
    }
124
125
    /**
126
     * Update assignment.
127
     *
128
     * @return array|mixed
129
     */
130 View Code Duplication
    protected function updateAssignment()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
    {
132
        $uri = str_replace('{assignment}', $this->assignment, config('forge-publish.update_assignment_uri'));
133
        $url = config('forge-publish.url') . $uri;
134
        try {
135
            $response = $this->http->put($url, [
136
                'form_params' => [
137
                    'name' => $this->assignmentName,
138
                    'repository_uri' => $this->repository_uri,
139
                    'repository_type' => $this->repository_type,
140
                    'forge_site' => $this->forge_site,
141
                    'forge_server' => $this->forge_server
142
                ],
143
                'headers' => [
144
                    'X-Requested-With' => 'XMLHttpRequest',
145
                    'Authorization' => 'Bearer ' . fp_env('ACACHA_FORGE_ACCESS_TOKEN')
146
                ]
147
            ]);
148
        } catch (\Exception $e) {
149
            $this->error('And error occurs connecting to the api url: ' . $url);
150
            $this->error('Status code: ' . $e->getResponse()->getStatusCode() . ' | Reason : ' . $e->getResponse()->getReasonPhrase());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Exception as the method getResponse() does only exist in the following sub-classes of Exception: GuzzleHttp\Exception\BadResponseException, GuzzleHttp\Exception\ClientException, GuzzleHttp\Exception\ConnectException, GuzzleHttp\Exception\RequestException, GuzzleHttp\Exception\ServerException, GuzzleHttp\Exception\TooManyRedirectsException. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
151
            return [];
152
        }
153
        return json_decode((string) $response->getBody());
154
    }
155
156
    /**
157
     * Abort command execution.
158
     */
159
    protected function abortCommandExecution()
160
    {
161
        $this->abortsIfEnvVarIsNotInstalled('ACACHA_FORGE_ACCESS_TOKEN');
162
    }
163
}
164