Completed
Push — master ( 1935ec...6fa257 )
by Nils
02:00
created

CrawlArticleCommand::setSesame()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Sesame\Command;
3
4
use GuzzleHttp\Client;
5
use Sesame\Sesame;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
/**
13
 * Class CrawlArticleCommand
14
 * @package Sesame\Command
15
 */
16
class CrawlArticleCommand extends Command
17
{
18
19
    /**
20
     * @var Sesame
21
     */
22
    protected $sesame;
23
24
    /**
25
     * CrawlArticleCommand constructor
26
     *
27
     * @param null|string $name
28
     */
29
    public function __construct(?string $name = null)
30
    {
31
        parent::__construct($name);
32
    }
33
34
    /**
35
     * setSesame
36
     * The setter for the dependency injection of the Sesame class.
37
     * @param Sesame $sesame
38
     */
39
    public function setSesame(Sesame $sesame)
40
    {
41
        $this->sesame = $sesame;
42
    }
43
44
    /**
45
     * configure
46
     */
47
    protected function configure()
48
    {
49
        $this
50
            ->setName('crawl:article')
51
            ->setDescription('Crawls an article.')
52
            ->setHelp('This command allows you to crawl an article on article on aliexpress.com and grab its data.')
53
            ->addArgument(
54
                'articleUrl',
55
                InputArgument::REQUIRED,
56
                'The URL of the article, which copied from your browser.'
57
            )
58
            ->addOption(
59
                'crawlVariations',
60
                'cv',
61
                InputOption::VALUE_REQUIRED,
62
                'With this option you can control if Sesame will crawl the variations for the article too. Syntax "-cv0" to ignore variations or "--crawlVariations=1" to include them.',
63
                1
64
            );
65
    }
66
67
    /**
68
     * execute
69
     * The actual business logic of the command.
70
     * @param InputInterface $input
71
     * @param OutputInterface $output
72
     * @return int|null|void
73
     */
74
    protected function execute(InputInterface $input, OutputInterface $output)
75
    {
76
        $articleUrl = $input->getArgument('articleUrl');
77
        $crawlVariations = $input->getOption('crawlVariations');
78
        if ($crawlVariations === 1) {
79
            $crawlVariations = true;
80
        } elseif ($crawlVariations === 0) {
81
            $crawlVariations = false;
82
        }
83
        $client = new Client();
84
        $response = $client->request('GET', $articleUrl);
85
        $responseBody = $response->getBody();
86
        $this->sesame->crawlArticle((string) $responseBody, $crawlVariations);
87
        print_r((string)$responseBody);
88
    }
89
}
90