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

Controller/Admin/Order/CsvImportController.php (2 issues)

mismatching argument types.

Documentation Minor

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\Controller\Admin\Order;
15
16
use Eccube\Controller\Admin\AbstractCsvImportController;
17
use Eccube\Entity\Master\OrderStatus;
18
use Eccube\Entity\Shipping;
19
use Eccube\Form\Type\Admin\CsvImportType;
20
use Eccube\Repository\ShippingRepository;
21
use Eccube\Service\CsvImportService;
22
use Eccube\Service\OrderStateMachine;
23
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\Routing\Annotation\Route;
26
27
class CsvImportController extends AbstractCsvImportController
28
{
29
    /**
30
     * @var ShippingRepository
31
     */
32
    private $shippingRepository;
33
34
    /**
35
     * @var OrderStateMachine
36
     */
37
    protected $orderStateMachine;
38
39 8
    public function __construct(
40
        ShippingRepository $shippingRepository,
41
        OrderStateMachine $orderStateMachine
42
    ) {
43 8
        $this->shippingRepository = $shippingRepository;
44 8
        $this->orderStateMachine = $orderStateMachine;
45
    }
46
47
    /**
48
     * 出荷CSVアップロード
49
     *
50
     * @Route("/%eccube_admin_route%/order/shipping_csv_upload", name="admin_shipping_csv_import")
51
     * @Template("@admin/Order/csv_shipping.twig")
52
     *
53
     * @throws \Doctrine\DBAL\ConnectionException
54
     */
55 1
    public function csvShipping(Request $request)
56
    {
57 1
        $form = $this->formFactory->createBuilder(CsvImportType::class)->getForm();
58 1
        $columnConfig = $this->getColumnConfig();
59 1
        $errors = [];
60
61 1
        if ($request->getMethod() === 'POST') {
62 1
            $form->handleRequest($request);
63 1
            if ($form->isValid()) {
64 1
                $formFile = $form['import_file']->getData();
65
66 1
                if (!empty($formFile)) {
67 1
                    $csv = $this->getImportData($formFile);
68
69
                    try {
70 1
                        $this->entityManager->getConfiguration()->setSQLLogger(null);
71 1
                        $this->entityManager->getConnection()->beginTransaction();
72
73 1
                        $this->loadCsv($csv, $errors);
74
75 1
                        if ($errors) {
76
                            $this->entityManager->getConnection()->rollBack();
77
                        } else {
78 1
                            $this->entityManager->flush();
79 1
                            $this->entityManager->getConnection()->commit();
80
81 1
                            $this->addInfo('admin.common.csv_upload_complete', 'admin');
82
                        }
83 1
                    } finally {
84 1
                        $this->removeUploadedFile();
85
                    }
86
                }
87
            }
88
        }
89
90
        return [
91 1
            'form' => $form->createView(),
92 1
            'headers' => $columnConfig,
93 1
            'errors' => $errors,
94
        ];
95
    }
96
97 8
    protected function loadCsv(CsvImportService $csv, &$errors)
98
    {
99 8
        $columnConfig = $this->getColumnConfig();
100
101 8
        if ($csv === false) {
102
            $errors[] = trans('admin.common.csv_invalid_format');
103
        }
104
105
        // 必須カラムの確認
106 8
        $requiredColumns = array_map(function ($value) {
107 8
            return $value['name'];
108
        }, array_filter($columnConfig, function ($value) {
109 8
            return $value['required'];
110 8
        }));
111 8
        $csvColumns = $csv->getColumnHeaders();
112 8
        if (count(array_diff($requiredColumns, $csvColumns)) > 0) {
113 2
            $errors[] = trans('admin.common.csv_invalid_format');
114
115
            return;
116
        }
117 8
118 8
        // 行数の確認
119
        $size = count($csv);
120
        if ($size < 1) {
121
            $errors[] = trans('admin.common.csv_invalid_format');
122 8
123
            return;
124 8
        }
125
126 8
        $columnNames = array_combine(array_keys($columnConfig), array_column($columnConfig, 'name'));
127 2
128 2
        foreach ($csv as $line => $row) {
129
            // 出荷IDがなければエラー
130
            if (!isset($row[$columnNames['id']])) {
131
                $errors[] = trans('admin.common.csv_invalid_required', ['%line%' => $line + 1, '%name%' => $columnNames['id']]);
132 6
                continue;
133
            }
134
135 6
            /* @var Shipping $Shipping */
136 2
            $Shipping = is_numeric($row[$columnNames['id']]) ? $this->shippingRepository->find($row[$columnNames['id']]) : null;
137 2
138
            // 存在しない出荷IDはエラー
139
            if (is_null($Shipping)) {
140 4
                $errors[] = trans('admin.common.csv_invalid_not_found', ['%line%' => $line + 1, '%name%' => $columnNames['id']]);
141 4
                continue;
142
            }
143
144 4
            if (isset($row[$columnNames['tracking_number']])) {
145
                $Shipping->setTrackingNumber($row[$columnNames['tracking_number']]);
146 4
            }
147 4
148 1
            if (isset($row[$columnNames['shipping_date']])) {
149 1
                // 日付フォーマットが異なる場合はエラー
150
                $shippingDate = \DateTime::createFromFormat('Y-m-d', $row[$columnNames['shipping_date']]);
151
                if ($shippingDate === false) {
152 3
                    $errors[] = trans('admin.common.csv_invalid_date_format', ['%line%' => $line + 1, '%name%' => $columnNames['shipping_date']]);
153 3
                    continue;
154
                }
155
156 3
                $shippingDate->setTime(0, 0, 0);
157 3
                $Shipping->setShippingDate($shippingDate);
158 3
            }
159 3
160 3
            $Order = $Shipping->getOrder();
161
            $RelateShippings = $Order->getShippings();
162 3
            $allShipped = true;
163
            foreach ($RelateShippings as $RelateShipping) {
164
                if (!$RelateShipping->getShippingDate()) {
165 3
                    $allShipped = false;
166 3
                    break;
167 3
                }
168 1
            }
169
            $OrderStatus = $this->entityManager->find(OrderStatus::class, OrderStatus::DELIVERED);
170 2
            if ($allShipped) {
171 2
                if ($this->orderStateMachine->can($Order, $OrderStatus)) {
0 ignored issues
show
$OrderStatus is of type object|null, but the function expects a object<Eccube\Entity\Master\OrderStatus>.

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...
172 3
                    $this->orderStateMachine->apply($Order, $OrderStatus);
0 ignored issues
show
$OrderStatus is of type object|null, but the function expects a object<Eccube\Entity\Master\OrderStatus>.

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...
173 View Code Duplication
                } else {
174
                    $from = $Order->getOrderStatus()->getName();
175
                    $to = $OrderStatus->getName();
176
                    $errors[] = trans('admin.order.failed_to_change_status', [
177
                        '%name%' => $Shipping->getId(),
178
                        '%from%' => $from,
179
                        '%to%' => $to,
180
                    ]);
181
                }
182
            }
183
        }
184
    }
185
186
    /**
187
     * アップロード用CSV雛形ファイルダウンロード
188
     *
189
     * @Route("/%eccube_admin_route%/order/csv_template", name="admin_shipping_csv_template")
190 8
     */
191
    public function csvTemplate(Request $request)
192
    {
193 8
        $columns = array_column($this->getColumnConfig(), 'name');
194 8
195 8
        return $this->sendTemplateResponse($request, $columns, 'shipping.csv');
196
    }
197
198
    protected function getColumnConfig()
199 8
    {
200 8
        return [
201
            'id' => [
202
                'name' => trans('admin.order.shipping_csv.shipping_id_col'),
203
                'description' => trans('admin.order.shipping_csv.shipping_id_description'),
204 8
                'required' => true,
205 8
            ],
206
            'tracking_number' => [
207
                'name' => trans('admin.order.shipping_csv.tracking_number_col'),
208
                'description' => trans('admin.order.shipping_csv.tracking_number_description'),
209
                'required' => false,
210
            ],
211
            'shipping_date' => [
212
                'name' => trans('admin.order.shipping_csv.shipping_date_col'),
213
                'description' => trans('admin.order.shipping_csv.shipping_date_description'),
214
                'required' => true,
215
            ],
216
        ];
217
    }
218
}
219