Completed
Pull Request — master (#1)
by Tyler
04:03
created

LicenseUpdate::getSelection()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 6
nop 2
1
<?php 
2
3
namespace Tylercd100\License\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\DB;
7
8
class LicenseUpdate extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'license:update';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Updates a license quantity';
23
24
    /**
25
     * Execute the console command.
26
     *
27
     * @return mixed
28
     */
29
    public function handle()
30
    {
31
        // Pick values from the database
32
        $owner_type_classname = $this->selectOwnerType();
33
        $license_classname = $this->selectLicense(['owner_type' => $owner_type_classname]);
34
        $owner_id = $this->selectOwnerId(['owner_type' => $owner_type_classname, 'license' => $license_classname]);
35
        
36
        // Build the License instance
37
        $model = with(new $owner_type_classname)->newQuery()->where(["id" => $owner_id])->firstOrFail();
38
        $license = new $license_classname($model);
39
40
        // Perform the update
41
        $license->set($this->getQuantity($license->maximum()));
42
43
        $this->success("Done!");
0 ignored issues
show
Documentation Bug introduced by
The method success does not exist on object<Tylercd100\License\Commands\LicenseUpdate>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
44
    }
45
46
    protected function getQuantity($current = 0)
47
    {
48
        return intval($this->ask("Please select a new maximum value for this license. The current maximum is {$current}."));
49
    }
50
51
    protected function selectLicense($where = [])
52
    {
53
        return $this->getSelection('license', $where);
54
    }
55
56
    protected function selectOwnerType($where = [])
57
    {
58
        return $this->getSelection('owner_type', $where);
59
    }
60
61
    protected function selectOwnerId($where = [])
62
    {
63
        return $this->getSelection('owner_id', $where);
64
    }
65
66
    final protected function getSelection($column, $where = [])
67
    {
68
        try {
69
            $options = DB::table('licenses')->where($where)->groupBy($column)->get([$column])->pluck($column);
70
            if(count($options) > 1) {
71
                $selection = $this->choice("Select a {$column}", $options);
72
            } else {
73
                $selection = $options[0];
74
            }
75
        } catch (\OutOfBoundsException $e) {
76
            throw new \Exception("Could not find a {$column}", 1, $e);
77
        }
78
79
        return $selection;
80
    }
81
}