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
27declare(strict_types=1);
28
29namespace PrestaShop\PrestaShop\Adapter\Product\QueryHandler;
30
31use PrestaShop\PrestaShop\Adapter\Product\Repository\ProductRepository;
32use PrestaShop\PrestaShop\Core\Domain\Product\Query\GetRelatedProducts;
33use PrestaShop\PrestaShop\Core\Domain\Product\QueryHandler\GetRelatedProductsHandlerInterface;
34use PrestaShop\PrestaShop\Core\Domain\Product\QueryResult\RelatedProduct;
35
36final class GetRelatedProductsHandler implements GetRelatedProductsHandlerInterface
37{
38    /**
39     * @var ProductRepository
40     */
41    private $productRepository;
42
43    /**
44     * @param ProductRepository $productRepository
45     */
46    public function __construct(
47        ProductRepository $productRepository
48    ) {
49        $this->productRepository = $productRepository;
50    }
51
52    /**
53     * {@inheritdoc}
54     */
55    public function handle(GetRelatedProducts $query): array
56    {
57        $results = $this->productRepository->getRelatedProducts($query->getProductId(), $query->getLanguageId());
58
59        $relatedProducts = [];
60
61        foreach ($results as $result) {
62            $relatedProducts[] = new RelatedProduct(
63                (int) $result['id_product'],
64                $result['name'],
65                $result['reference']
66            );
67        }
68
69        return $relatedProducts;
70    }
71}
72