Completed
Push — 4.0 ( 268f2c...88f012 )
by Hideki
05:48 queued 10s
created

src/Eccube/Command/DeleteCartsCommand.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.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
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);
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);
0 ignored issues
show
It seems like $dateStr defined by $input->getArgument('date') on line 115 can also be of type array<integer,string> or null; however, Symfony\Component\Intl\D...lDateFormatter::parse() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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,
155
            \IntlDateFormatter::GREGORIAN
156
        );
157
    }
158
}
159