aboutsummaryrefslogtreecommitdiffhomepage
path: root/api/Posts/Console/AddPostCommand.php
blob: 977afb60b2098fa68d292283beef79df6fe3601e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php

namespace Api\Posts\Console;

use Api\Posts\Repositories\PostRepository;
use Illuminate\Console\Command;

class AddPostCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'posts:add {name} {email} {password}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Adds a new post';

    /**
     * Post repository to persist post in database
     *
     * @var PostRepository
     */
    protected $postRepository;

    /**
     * Create a new command instance.
     *
     * @param  PostRepository  $postRepository
     * @return void
     */
    public function __construct(PostRepository $postRepository)
    {
        parent::__construct();

        $this->postRepository = $postRepository;
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $post = $this->postRepository->create([
            'name' => $this->argument('name'),
            'email' => $this->argument('email'),
            'password' => $this->argument('password')
        ]);

        $this->info(sprintf('A post was created with ID %s', $post->id));
    }
}