Code Duplication    Length = 76-76 lines in 2 locations

src/FlashCard/Command/CreateCard.php 1 location

@@ 16-91 (lines=76) @@
13
 *
14
 * @author Mohamed Alsharaf <[email protected]>
15
 */
16
class CreateCard extends Command
17
{
18
    use AskAndValidate;
19
20
    /**
21
     * The name and signature of the console command.
22
     *
23
     * @var string
24
     */
25
    protected $signature = 'flashcard:card {--A|active= : If set, the card is going to be active.}';
26
27
    /**
28
     * The console command description.
29
     *
30
     * @var string
31
     */
32
    protected $description = 'Create a card';
33
34
    /**
35
     * @var Card
36
     */
37
    protected $entity;
38
39
40
    /**
41
     * Instance of card model
42
     *
43
     * @return Model
44
     */
45
    protected function getEntity(): Model
46
    {
47
        if (is_null($this->entity)) {
48
            $this->entity = new Card([
49
                'active' => (bool) $this->option('active'),
50
            ]);
51
        }
52
53
        return $this->entity;
54
    }
55
56
    /**
57
     * Execute the command line to create a new card.
58
     *
59
     * @return int
60
     */
61
    public function handle()
62
    {
63
        // Get collection of categories - can complete process if no categories found
64
        $choices = Category::all()->pluck('title', 'id')->toArray();
65
        if (count($choices) === 0) {
66
            $this->error('Add category first before creating cards.');
67
68
            return 1;
69
        }
70
71
        // Ask for card title
72
        $this->askWithValidation('Please enter card title', 'title');
73
74
        // Ask for card content
75
        $this->askWithValidation('Please enter card content', 'content');
76
77
        // Ask for card category & set card category
78
        $category = $this->choice('Please select card category', $choices, key($choices));
79
        $this->getEntity()->fill([
80
            'category_id' => array_search($category, $choices, true),
81
        ]);
82
83
        // Ask for card meta description
84
        $this->askWithValidation('Please enter card SEO meta description', 'meta_description');
85
86
        // Save entity and display message
87
        $this->getEntity()->save();
88
        $this->info('Voila... You have created a new card.');
89
90
        return 0;
91
    }
92
}
93

src/FlashCard/Command/CreateCategory.php 1 location

@@ 15-90 (lines=76) @@
12
 *
13
 * @author Mohamed Alsharaf <[email protected]>
14
 */
15
class CreateCategory extends Command
16
{
17
    use AskAndValidate;
18
19
    /**
20
     * The name and signature of the console command.
21
     *
22
     * @var string
23
     */
24
    protected $signature = 'flashcard:category {--A|active= : If set, the category is going to be active.}';
25
26
    /**
27
     * The console command description.
28
     *
29
     * @var string
30
     */
31
    protected $description = 'Create a category';
32
33
    /**
34
     * @var Category
35
     */
36
    protected $entity;
37
38
39
    /**
40
     * Instance of category model
41
     *
42
     * @return Model
43
     */
44
    protected function getEntity(): Model
45
    {
46
        if (is_null($this->entity)) {
47
            $this->entity = new Category([
48
                'active' => (bool) $this->option('active'),
49
            ]);
50
        }
51
52
        return $this->entity;
53
    }
54
55
    /**
56
     * Execute the command line to create a new category.
57
     *
58
     * @return int
59
     */
60
    public function handle()
61
    {
62
        // Ask for category title
63
        $this->askWithValidation('Please enter category title', 'title');
64
65
        // Ask for category content
66
        $this->askWithValidation('Please enter category description', 'description');
67
68
        // Ask for category color
69
        $this->askWithValidation('Please enter category color', 'color');
70
71
        // Ask for card category
72
        $choices = Category::all()->pluck('title', 'id')->prepend('Root Category')->toArray();
73
        if (count($choices) > 0) {
74
            $category = $this->choice('Please select category parent or enter "0" for root category', $choices, key($choices));
75
76
            // Set parent category id if value not "0"
77
            if ($category !== '0') {
78
                $this->getEntity()->fill([
79
                    'parent' => array_search($category, $choices, true),
80
                ]);
81
            }
82
        }
83
84
        // Save entity and display message
85
        $this->getEntity()->save();
86
        $this->info('Voila... You have created a new category.');
87
88
        return 0;
89
    }
90
}
91