Failed Conditions
Branch experimental/sf (68db07)
by Kentaro
42:17 queued 33:39
created

DeleteCartsCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 134
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A configure() 0 6 1
A interact() 0 25 2
A initialize() 0 7 1
A execute() 0 10 1
A deleteCarts() 0 23 2
A createIntlFormatter() 0 10 1
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Command;
15
16
use Doctrine\ORM\EntityManagerInterface;
17
use Eccube\Repository\CartRepository;
18
use Symfony\Component\Console\Command\Command;
19
use Symfony\Component\Console\Input\InputArgument;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Output\OutputInterface;
22
use Symfony\Component\Console\Style\SymfonyStyle;
23
use Symfony\Component\DependencyInjection\ContainerInterface;
24
25
class DeleteCartsCommand extends Command
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
26
{
27
    protected static $defaultName = 'eccube:delete-carts';
28
29
    /**
30
     * @var ContainerInterface
31
     */
32
    protected $container;
33
34
    /**
35
     * @var SymfonyStyle
36
     */
37
    protected $io;
38
39
    /**
40
     * @var string
41
     */
42
    protected $locale;
43
44
    /**
45
     * @var \DateTimeZone
46
     */
47
    protected $timezone;
48
49
    /**
50
     * @var \IntlDateFormatter
51
     */
52
    protected $formatter;
53
54
    /**
55
     * @var EntityManagerInterface
56
     */
57
    protected $entityManager;
58
    /**
59
     * @var CartRepository
60
     */
61
    private $cartRepository;
62
63
    public function __construct(ContainerInterface $container, EntityManagerInterface $entityManager, CartRepository $cartRepository)
64
    {
65
        parent::__construct();
66
67
        $this->container = $container;
68
        $this->entityManager = $entityManager;
69
        $this->cartRepository = $cartRepository;
70
    }
71
72
    protected function configure()
73
    {
74
        $this
75
            ->setDescription('Delete Carts from the database')
76
            ->addArgument('date', InputArgument::REQUIRED, 'Deletes carts before the specified date');
77
    }
78
79
    protected function interact(InputInterface $input, OutputInterface $output)
80
    {
81
        if (null !== $input->getArgument('date')) {
82
            return;
83
        }
84
85
        $pattern = $this->formatter->getPattern();
86
        $this->io->title('Delete Cart Command Interactive Wizard');
87
        $this->io->text([
88
            'If you prefer to not use this interactive wizard, provide the',
89
            'arguments required by this command as follows:',
90
            '',
91
            ' $ php bin/console eccube:delete-cart <'.$pattern.'>',
92
            '',
93
            'Now we\'ll ask you for the value of all the missing command arguments.',
94
            '',
95
        ]);
96
97
        $now = new \DateTime('now', $this->timezone);
98
99
        $dateStr = $this->formatter->format($now->getTimestamp());
100
        $dateStr = $this->io->ask('date', $dateStr);
0 ignored issues
show
Security Bug introduced by
It seems like $dateStr defined by $this->io->ask('date', $dateStr) on line 100 can also be of type false; however, Symfony\Component\Consol...yle\SymfonyStyle::ask() does only seem to accept string|null, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
101
102
        $input->setArgument('date', $dateStr);
103
    }
104
105
    protected function initialize(InputInterface $input, OutputInterface $output)
106
    {
107
        $this->io = new SymfonyStyle($input, $output);
108
        $this->locale = $this->container->getParameter('locale');
109
        $this->timezone = new \DateTimeZone($this->container->getParameter('timezone'));
110
        $this->formatter = $this->createIntlFormatter();
111
    }
112
113
    protected function execute(InputInterface $input, OutputInterface $output)
114
    {
115
        $dateStr = $input->getArgument('date');
116
        $timestamp = $this->formatter->parse($dateStr);
117
        $dateTime = new \DateTime("@$timestamp", $this->timezone);
118
119
        $this->deleteCarts($dateTime);
120
121
        $this->io->success('Delete carts successful.');
122
    }
123
124
    protected function deleteCarts(\DateTime $dateTime)
125
    {
126
        try {
127
            $this->entityManager->beginTransaction();
128
129
            $qb = $this->cartRepository->createQueryBuilder('c')
130
                ->delete()
131
                ->where('c.update_date <= :date')
132
                ->setParameter('date', $dateTime);
133
134
            $deleteRows = $qb->getQuery()->getResult();
135
136
            $this->entityManager->flush();
137
            $this->entityManager->commit();
138
139
            $this->io->comment("Deleted ${deleteRows} carts.");
140
        } catch (\Exception $e) {
141
            $this->io->error('Failed delete carts. Rollbacked.');
142
            $this->entityManager->rollback();
143
        }
144
145
        return;
146
    }
147
148
    protected function createIntlFormatter()
149
    {
150
        return \IntlDateFormatter::create(
151
            $this->locale,
152
            \IntlDateFormatter::MEDIUM,
153
            \IntlDateFormatter::NONE,
154
            $this->timezone,
0 ignored issues
show
Documentation introduced by
$this->timezone is of type object<DateTimeZone>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
155
            \IntlDateFormatter::GREGORIAN
156
        );
157
    }
158
}
159