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\Core\Domain\Category\ValueObject;
28
29use PrestaShop\PrestaShop\Core\Domain\Category\Exception\CategoryConstraintException;
30
31/**
32 * Class CategoryDeleteMode stores mode for category deletion.
33 */
34class CategoryDeleteMode
35{
36    /**
37     * Associate products with parent category and disable them.
38     */
39    public const ASSOCIATE_PRODUCTS_WITH_PARENT_AND_DISABLE = 'associate_and_disable';
40
41    /**
42     * Associate products with parent and do not change their status.
43     */
44    public const ASSOCIATE_PRODUCTS_WITH_PARENT_ONLY = 'associate_only';
45
46    /**
47     * Remove products that are associated only with category that is being deleted.
48     */
49    public const REMOVE_ASSOCIATED_PRODUCTS = 'remove_associated';
50
51    /**
52     * @internal
53     */
54    public const AVAILABLE_MODES = [
55        self::ASSOCIATE_PRODUCTS_WITH_PARENT_AND_DISABLE,
56        self::ASSOCIATE_PRODUCTS_WITH_PARENT_ONLY,
57        self::REMOVE_ASSOCIATED_PRODUCTS,
58    ];
59
60    /**
61     * @var string
62     */
63    private $mode;
64
65    /**
66     * @param string $mode
67     *
68     * @throws CategoryConstraintException
69     */
70    public function __construct($mode)
71    {
72        $this->setMode($mode);
73    }
74
75    /**
76     * @param string $mode
77     *
78     * @throws CategoryConstraintException
79     */
80    private function setMode($mode)
81    {
82        if (!in_array($mode, self::AVAILABLE_MODES)) {
83            throw new CategoryConstraintException(sprintf('Invalid Category delete mode %s supplied. Available delete modes are: "%s"', var_export($mode, true), implode(',', self::AVAILABLE_MODES)), CategoryConstraintException::INVALID_DELETE_MODE);
84        }
85
86        $this->mode = $mode;
87    }
88
89    /**
90     * Whether products associated with category should be removed.
91     *
92     * @return bool
93     */
94    public function shouldRemoveProducts()
95    {
96        return self::REMOVE_ASSOCIATED_PRODUCTS === $this->mode;
97    }
98
99    /**
100     * Whether products should be disabled when category is deleted.
101     *
102     * @return bool
103     */
104    public function shouldDisableProducts()
105    {
106        return self::ASSOCIATE_PRODUCTS_WITH_PARENT_AND_DISABLE === $this->mode;
107    }
108}
109