PublishAssignment::handle()   D
last analyzed

Complexity

Conditions 8
Paths 128

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 22
nc 128
nop 0
dl 0
loc 31
rs 4.6666
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 GuzzleHttp\Client;
10
use Illuminate\Console\Command;
11
12
/**
13
 * Class PublishAssignment.
14
 *
15
 * @package Acacha\ForgePublish\Commands
16
 */
17
class PublishAssignment extends Command
18
{
19
20
    use InteractsWithLocalGithub, InteractsWithEnvironment, AborstIfEnvVariableIsnotInstalled, InteractsWithAssignments;
21
22
    /**
23
     * The name and signature of the console command.
24
     *
25
     * @var string
26
     */
27
    protected $signature = 'publish:assignment {name? : The name description} 
28
                                               {repository_uri? : The repository URI} 
29
                                               {repository_type? : The repository type} 
30
                                               {forge_site? : The Laravel Forge site id} 
31
                                               {forge_server? : The Laravel Forge Server}';
32
33
    /**
34
     * The console command description.
35
     *
36
     * @var string
37
     */
38
    protected $description = 'Create a new assignment using current project';
39
40
    /**
41
     * Server names.
42
     *
43
     * @var Client
44
     */
45
    protected $http;
46
47
    /**
48
     * Assignment name.
49
     *
50
     * @var String
51
     */
52
    protected $assignmentName;
53
54
    /**
55
     * Repository uri.
56
     *
57
     * @var String
58
     */
59
    protected $repository_uri;
60
61
    /**
62
     * Repository type.
63
     *
64
     * @var String
65
     */
66
    protected $repository_type;
67
68
    /**
69
     * Laravel Forge site id.
70
     *
71
     * @var String
72
     */
73
    protected $forge_site;
74
75
    /**
76
     * Existing assignment
77
     * @var
78
     */
79
    protected $existingAssignment;
80
81
    /**
82
     * Laravel Forge server id.
83
     *
84
     * @var String
85
     */
86
    protected $forge_server;
87
88
    /**
89
     * SaveEnvVariable constructor.
90
     *
91
     */
92
    public function __construct(Client $http)
93
    {
94
        parent::__construct();
95
        $this->http = $http;
96
    }
97
98
    /**
99
     * Execute the console command.
100
     *
101
     */
102
    public function handle()
103
    {
104
        $this->abortCommandExecution();
105
106
        $this->existingAssignment = fp_env('ACACHA_FORGE_ASSIGNMENT');
107
108
        if ($this->existingAssignment) $this->error('An assignment with id : ' . $this->existingAssignment . ' already exists! Updating...' );
109
110
        $this->assignmentName = $this->argument('name') ? $this->argument('name') : $this->askName();
111
        $this->repository_uri = $this->argument('repository_uri') ? $this->argument('repository_uri') : $this->askRepositoryUri();
112
        $this->repository_type = $this->argument('repository_type') ? $this->argument('repository_type') : $this->askRepositoryType();
113
        $this->forge_site = $this->argument('forge_site') ? $this->argument('forge_site') : $this->askForgeSite();
114
        $this->forge_server = $this->argument('forge_server') ? $this->argument('forge_server') : $this->askForgeServer();
115
116
        if (! $this->existingAssignment) {
117
            $this->createAssignment();
118
            $this->info('Assignment created ok!');
119
        } else {
120
            $this->call('publish:update_assignment', [
121
               'assignment' => $this->existingAssignment,
122
               'name' => $this->assignmentName,
123
               'repository_uri' => $this->repository_uri,
124
               'repository_type' => $this->repository_type,
125
               'forge_site' => $this->forge_site,
126
               'forge_server' => $this->forge_server,
127
            ]);
128
        }
129
130
        $this->call('publish:assignment_groups');
131
        $this->call('publish:assignment_users');
132
    }
133
134
    /**
135
     * Create assignment.
136
     *
137
     * @return array|mixed
138
     */
139 View Code Duplication
    protected function createAssignment()
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...
140
    {
141
        $url = config('forge-publish.url') . config('forge-publish.store_assignment_uri');
142
        try {
143
            $response = $this->http->post($url, [
144
                'form_params' => [
145
                    'name' => $this->assignmentName,
146
                    'repository_uri' => $this->repository_uri,
147
                    'repository_type' => $this->repository_type,
148
                    'forge_site' => $this->forge_site,
149
                    'forge_server' => $this->forge_server
150
                ],
151
                'headers' => [
152
                    'X-Requested-With' => 'XMLHttpRequest',
153
                    'Authorization' => 'Bearer ' . fp_env('ACACHA_FORGE_ACCESS_TOKEN')
154
                ]
155
            ]);
156
        } catch (\Exception $e) {
157
            $this->error('And error occurs connecting to the api url: ' . $url);
158
            $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...
159
            return;
160
        }
161
        $assignment = json_decode((string) $response->getBody());
162
        $this->addValueToEnv('ACACHA_FORGE_ASSIGNMENT', $assignment->id);
163
        return $assignment;
164
    }
165
166
    /**
167
     * Abort command execution.
168
     */
169
    protected function abortCommandExecution()
170
    {
171
        $this->abortsIfEnvVarIsNotInstalled('ACACHA_FORGE_ACCESS_TOKEN');
172
    }
173
}
174