Completed
Push — develop ( ee7181...006e98 )
by Jens
08:57
created

ProductData   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 55
ccs 12
cts 12
cp 1
rs 10
c 1
b 0
f 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fieldDefinitions() 0 16 1
A getVariantById() 0 8 2
A getVariantBySku() 0 8 2
A getAllVariants() 0 6 1
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 * @created: 09.02.15, 10:48
5
 */
6
7
namespace Commercetools\Core\Model\Product;
8
9
use Commercetools\Core\Model\Category\CategoryReferenceCollection;
10
use Commercetools\Core\Model\Common\JsonObject;
11
use Commercetools\Core\Model\Common\LocalizedString;
12
use Commercetools\Core\Model\State\StateReference;
13
14
/**
15
 * @package Commercetools\Core\Model\Product
16
 * @link https://dev.commercetools.com/http-api-projects-products.html#product-data
17
 * @method LocalizedString getName()
18
 * @method ProductData setName(LocalizedString $name = null)
19
 * @method LocalizedString getDescription()
20
 * @method ProductData setDescription(LocalizedString $description = null)
21
 * @method LocalizedString getSlug()
22
 * @method ProductData setSlug(LocalizedString $slug = null)
23
 * @method CategoryReferenceCollection getCategories()
24
 * @method ProductData setCategories(CategoryReferenceCollection $categories = null)
25
 * @method array getCategoryOrderHints()
26
 * @method ProductData setCategoryOrderHints(array $categoryOrderHints = null)
27
 * @method LocalizedString getMetaTitle()
28
 * @method ProductData setMetaTitle(LocalizedString $metaTitle = null)
29
 * @method LocalizedString getMetaDescription()
30
 * @method ProductData setMetaDescription(LocalizedString $metaDescription = null)
31
 * @method LocalizedString getMetaKeywords()
32
 * @method ProductData setMetaKeywords(LocalizedString $metaKeywords = null)
33
 * @method ProductVariant getMasterVariant()
34
 * @method ProductData setMasterVariant(ProductVariant $masterVariant = null)
35
 * @method ProductVariantCollection getVariants()
36
 * @method ProductData setVariants(ProductVariantCollection $variants = null)
37
 * @method LocalizedSearchKeywords getSearchKeywords()
38
 * @method ProductData setSearchKeywords(LocalizedSearchKeywords $searchKeywords = null)
39
 */
40
class ProductData extends JsonObject
41
{
42 30
    public function fieldDefinitions()
43
    {
44
        return [
45 30
            'name' => [static::TYPE => '\Commercetools\Core\Model\Common\LocalizedString'],
46 30
            'description' => [static::TYPE => '\Commercetools\Core\Model\Common\LocalizedString'],
47 30
            'slug' => [static::TYPE => '\Commercetools\Core\Model\Common\LocalizedString'],
48 30
            'categories' => [static::TYPE => '\Commercetools\Core\Model\Category\CategoryReferenceCollection'],
49 30
            'categoryOrderHints' => [static::TYPE => 'array'],
50 30
            'metaTitle' => [static::TYPE => '\Commercetools\Core\Model\Common\LocalizedString'],
51 30
            'metaDescription' => [static::TYPE => '\Commercetools\Core\Model\Common\LocalizedString'],
52 30
            'metaKeywords' => [static::TYPE => '\Commercetools\Core\Model\Common\LocalizedString'],
53 30
            'masterVariant' => [static::TYPE => '\Commercetools\Core\Model\Product\ProductVariant'],
54 30
            'variants' => [static::TYPE => '\Commercetools\Core\Model\Product\ProductVariantCollection'],
55 30
            'searchKeywords' => [static::TYPE => '\Commercetools\Core\Model\Product\LocalizedSearchKeywords'],
56
        ];
57
    }
58
59
    /**
60
     * @param $variantId
61
     * @return ProductVariant|null
62
     */
63
    public function getVariantById($variantId)
64
    {
65
        if ($variantId == $this->getMasterVariant()->getId()) {
66
            return $this->getMasterVariant();
67
        }
68
69
        return $this->getVariants()->getById($variantId);
70
    }
71
72
    /**
73
     * @param $sku
74
     * @return ProductVariant
75
     */
76
    public function getVariantBySku($sku)
77
    {
78
        if ($sku == $this->getMasterVariant()->getSku()) {
79
            return $this->getMasterVariant();
80
        }
81
82
        return $this->getVariants()->getBySku($sku);
83
    }
84
85
    /**
86
     * @return ProductVariantCollection
87
     */
88
    public function getAllVariants()
89
    {
90
        $variants = $this->getRaw('variants', []);
91
        array_unshift($variants, $this->getRaw('masterVariant'));
92
        return ProductVariantCollection::fromArray($variants, $this->getContextCallback());
93
    }
94
}
95