Completed
Branch master (beb88f)
by Mohamed
05:11 queued 03:39
created

CreateCard::handle()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 14

Duplication

Lines 30
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 30
loc 30
ccs 0
cts 18
cp 0
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 14
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Moo\FlashCard\Command;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Database\Eloquent\Model;
7
use Moo\FlashCard\Entity\Card;
8
use Moo\FlashCard\Entity\Category;
9
use Moo\FlashCard\Traits\AskAndValidate;
10
11
/**
12
 * CreateCardCommand is a command line class for creating a new card.
13
 *
14
 * @author Mohamed Alsharaf <[email protected]>
15
 */
16 View Code Duplication
class CreateCard extends Command
0 ignored issues
show
Duplication introduced by
This class 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...
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' => (boolean)$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
            return 1;
68
        }
69
70
        // Ask for card title
71
        $this->askWithValidation('Please enter card title', 'title');
72
73
        // Ask for card content
74
        $this->askWithValidation('Please enter card content', 'content');
75
76
        // Ask for card category & set card category
77
        $category = $this->choice('Please select card category', $choices, key($choices));
78
        $this->getEntity()->fill([
79
            'category_id' => array_search($category, $choices, true),
80
        ]);
81
82
        // Ask for card meta description
83
        $this->askWithValidation('Please enter card SEO meta description', 'meta_description');
84
85
        // Save entity and display message
86
        $this->getEntity()->save();
87
        $this->info('Voila... You have created a new card.');
88
89
        return 0;
90
    }
91
}
92