1<?php
2/**
3 * Copyright since 2007 PrestaShop SA and Contributors
4 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5 *
6 * NOTICE OF LICENSE
7 *
8 * This source file is subject to the Open Software License (OSL 3.0)
9 * that is bundled with this package in the file LICENSE.md.
10 * It is also available through the world-wide-web at this URL:
11 * https://opensource.org/licenses/OSL-3.0
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@prestashop.com so we can send you a copy immediately.
15 *
16 * DISCLAIMER
17 *
18 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
19 * versions in the future. If you wish to customize PrestaShop for your
20 * needs please refer to https://devdocs.prestashop.com/ for more information.
21 *
22 * @author    PrestaShop SA and Contributors <contact@prestashop.com>
23 * @copyright Since 2007 PrestaShop SA and Contributors
24 * @license   https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
25 */
26
27namespace PrestaShop\PrestaShop\Adapter\OrderMessage\CommandHandler;
28
29use OrderMessage;
30use PrestaShop\PrestaShop\Core\Domain\OrderMessage\Command\AddOrderMessageCommand;
31use PrestaShop\PrestaShop\Core\Domain\OrderMessage\CommandHandler\AddOrderMessageHandlerInterface;
32use PrestaShop\PrestaShop\Core\Domain\OrderMessage\Exception\OrderMessageException;
33use PrestaShop\PrestaShop\Core\Domain\OrderMessage\Exception\OrderMessageNameAlreadyUsedException;
34use PrestaShop\PrestaShop\Core\Domain\OrderMessage\ValueObject\OrderMessageId;
35use PrestaShopException;
36
37/**
38 * Handles adding new order message using legacy object model
39 *
40 * @internal
41 */
42final class AddOrderMessageHandler implements AddOrderMessageHandlerInterface
43{
44    /**
45     * @param AddOrderMessageCommand $command
46     *
47     * @return OrderMessageId
48     */
49    public function handle(AddOrderMessageCommand $command): OrderMessageId
50    {
51        $this->assertNameIsNotAlreadyUsed($command);
52
53        $orderMessage = new OrderMessage();
54
55        $orderMessage->name = $command->getLocalizedName();
56        $orderMessage->message = $command->getLocalizedMessage();
57
58        try {
59            $orderMessage->validateFields();
60            $orderMessage->validateFieldsLang();
61        } catch (PrestaShopException $e) {
62            throw new OrderMessageException('Order message contains invalid fields', 0, $e);
63        }
64
65        try {
66            if (false === $orderMessage->add()) {
67                throw new OrderMessageException('Failed to add order message');
68            }
69        } catch (PrestaShopException $e) {
70            throw new OrderMessageException('Failed to add order message', 0, $e);
71        }
72
73        return new OrderMessageId((int) $orderMessage->id);
74    }
75
76    private function assertNameIsNotAlreadyUsed(AddOrderMessageCommand $command): void
77    {
78        foreach ($command->getLocalizedName() as $langId => $langName) {
79            $orderMessages = OrderMessage::getOrderMessages($langId);
80            if (!is_array($orderMessages)) {
81                continue;
82            }
83            foreach ($orderMessages as $orderMessage) {
84                if ($orderMessage['name'] === $langName) {
85                    throw new OrderMessageNameAlreadyUsedException(
86                        $langName,
87                        $langId,
88                        'An order message already exists for this name'
89                    );
90                }
91            }
92        }
93    }
94}
95