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

ProductData::getAllVariants()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
c 1
b 0
f 1
cc 1
eloc 4
nc 1
nop 0
crap 2
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