CatalogUrlPathProvider::getProductUrlPath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Tkotosz\CatalogRouter\Model\Service;
4
5
use Tkotosz\CatalogRouter\Api\CatalogUrlPathProviderInterface;
6
use Tkotosz\CatalogRouter\Api\CategoryResolverInterface;
7
use Tkotosz\CatalogRouter\Api\ProductResolverInterface;
8
use Tkotosz\CatalogRouter\Model\UrlPath;
9
10
class CatalogUrlPathProvider implements CatalogUrlPathProviderInterface
11
{
12
    /**
13
     * @var CategoryResolverInterface
14
     */
15
    private $categoryResolver;
16
    
17
    /**
18
     * @var ProductResolverInterface
19
     */
20
    private $productResolver;
21
    
22
    /**
23
     * @param CategoryResolverInterface $categoryResolver
24
     * @param ProductResolverInterface  $productResolver
25
     */
26
    public function __construct(CategoryResolverInterface $categoryResolver, ProductResolverInterface $productResolver)
27
    {
28
        $this->categoryResolver = $categoryResolver;
29
        $this->productResolver = $productResolver;
30
    }
31
    
32
    /**
33
     * @param int $categoryId
34
     * @param int $storeId
35
     *
36
     * @return UrlPath
37
     */
38
    public function getCategoryUrlPath(int $categoryId, int $storeId) : UrlPath
39
    {
40
        $urlPath = '';
41
42
        foreach ($this->categoryResolver->resolveParentIds($categoryId, $storeId) as $parentCategoryId) {
43
            $urlPath .= '/' . $this->categoryResolver->resolveById($parentCategoryId, $storeId)->getUrlKey();
44
        }
45
46
        $urlPath .= '/' . $this->categoryResolver->resolveById($categoryId, $storeId)->getUrlKey();
47
48
        return new UrlPath($urlPath);
49
    }
50
51
    /**
52
     * @param int $productId
53
     * @param int $storeId
54
     *
55
     * @return UrlPath
56
     */
57
    public function getProductUrlPath(int $productId, int $storeId) : UrlPath
58
    {
59
        $urlPath = $this->productResolver->resolveById($productId, $storeId)->getUrlKey();
60
61
        return new UrlPath($urlPath);
62
    }
63
}
64