Passed
Push — main ( 8be512...d37a54 )
by Seth
04:30 queued 01:53
created

ActivateEntityCommand::handle()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 17
rs 10
1
<?php
2
3
namespace SaasReady\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Database\Eloquent\Model;
7
use SaasReady\Constants\CurrencyCode;
8
use SaasReady\Constants\LanguageCode;
9
use SaasReady\Models\Currency;
0 ignored issues
show
Bug introduced by
The type SaasReady\Models\Currency was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use RuntimeException;
11
use BackedEnum;
0 ignored issues
show
Bug introduced by
The type BackedEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use SaasReady\Models\Language;
0 ignored issues
show
Bug introduced by
The type SaasReady\Models\Language was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
class ActivateEntityCommand extends Command
15
{
16
    protected $signature = 'saas-ready:activate-entity {entity} {code}';
17
    protected $description = 'Activate an entity';
18
19
    public function handle(): int
20
    {
21
        $entity = $this->getEntity();
22
23
        if ($entity === null) {
24
            $this->error('Entity with the ' . $this->argument('code') . ' identifier does not exists');
25
26
            return 1;
27
        }
28
29
        $entity->update([
30
            'activated_at' => now(),
31
        ]);
32
33
        $this->info('Activated the entity ' . ($entity->code instanceof BackedEnum ? $entity->code->value : $entity->code));
0 ignored issues
show
Bug introduced by
The property value does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
34
35
        return 0;
36
    }
37
38
    public function getEntity(): ?Model
39
    {
40
        $code = $this->argument('code');
41
42
        return match ($this->argument('entity')) {
43
            'currency' => Currency::findByCode(CurrencyCode::tryFrom($code)),
44
            'language' => Language::findByCode(LanguageCode::tryFrom($code)),
45
            default => throw new RuntimeException('Unsupported entity'),
46
        };
47
    }
48
}
49