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! " );
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+ }
0 commit comments