Completed
Push — develop ( 59e20d...68d263 )
by Jens
09:59
created

ProductProjection::getReference()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
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\Common\Reference;
13
use Commercetools\Core\Model\Common\ReferenceObjectInterface;
14
use Commercetools\Core\Model\ProductType\ProductTypeReference;
15
use Commercetools\Core\Model\TaxCategory\TaxCategoryReference;
16
use Commercetools\Core\Model\Common\DateTimeDecorator;
17
use Commercetools\Core\Model\Review\ReviewRatingStatistics;
18
use Commercetools\Core\Model\State\StateReference;
19
20
/**
21
 * @package Commercetools\Core\Model\Product
22
 * @link https://dev.commercetools.com/http-api-projects-products.html#product-projection
23
 * @method string getId()
24
 * @method ProductProjection setId(string $id = null)
25
 * @method int getVersion()
26
 * @method ProductProjection setVersion(int $version = null)
27
 * @method DateTimeDecorator getCreatedAt()
28
 * @method ProductProjection setCreatedAt(\DateTime $createdAt = null)
29
 * @method DateTimeDecorator getLastModifiedAt()
30
 * @method ProductProjection setLastModifiedAt(\DateTime $lastModifiedAt = null)
31
 * @method ProductTypeReference getProductType()
32
 * @method ProductProjection setProductType(ProductTypeReference $productType = null)
33
 * @method LocalizedString getName()
34
 * @method ProductProjection setName(LocalizedString $name = null)
35
 * @method LocalizedString getDescription()
36
 * @method ProductProjection setDescription(LocalizedString $description = null)
37
 * @method LocalizedString getSlug()
38
 * @method ProductProjection setSlug(LocalizedString $slug = null)
39
 * @method CategoryReferenceCollection getCategories()
40
 * @method ProductProjection setCategories(CategoryReferenceCollection $categories = null)
41
 * @method LocalizedString getMetaTitle()
42
 * @method ProductProjection setMetaTitle(LocalizedString $metaTitle = null)
43
 * @method LocalizedString getMetaDescription()
44
 * @method ProductProjection setMetaDescription(LocalizedString $metaDescription = null)
45
 * @method LocalizedString getMetaKeywords()
46
 * @method ProductProjection setMetaKeywords(LocalizedString $metaKeywords = null)
47
 * @method bool getHasStagedChanges()
48
 * @method ProductProjection setHasStagedChanges(bool $hasStagedChanges = null)
49
 * @method bool getPublished()
50
 * @method ProductProjection setPublished(bool $published = null)
51
 * @method ProductVariant getMasterVariant()
52
 * @method ProductProjection setMasterVariant(ProductVariant $masterVariant = null)
53
 * @method ProductVariantCollection getVariants()
54
 * @method ProductProjection setVariants(ProductVariantCollection $variants = null)
55
 * @method TaxCategoryReference getTaxCategory()
56
 * @method ProductProjection setTaxCategory(TaxCategoryReference $taxCategory = null)
57
 * @method LocalizedSearchKeywords getSearchKeywords()
58
 * @method ProductProjection setSearchKeywords(LocalizedSearchKeywords $searchKeywords = null)
59
 * @method ReviewRatingStatistics getReviewRatingStatistics()
60
 * @method ProductProjection setReviewRatingStatistics(ReviewRatingStatistics $reviewRatingStatistics = null)
61
 * @method StateReference getState()
62
 * @method ProductProjection setState(StateReference $state = null)
63
 * @method array getCategoryOrderHints()
64
 * @method ProductProjection setCategoryOrderHints(array $categoryOrderHints = null)
65
 */
66
class ProductProjection extends JsonObject implements ReferenceObjectInterface
67
{
68 3
    public function fieldDefinitions()
69
    {
70
        return [
71 3
            'id' => [static::TYPE => 'string'],
72 3
            'version' => [static::TYPE => 'int'],
73
            'createdAt' => [
74 3
                static::TYPE => '\DateTime',
75 3
                static::DECORATOR => '\Commercetools\Core\Model\Common\DateTimeDecorator'
76
            ],
77
            'lastModifiedAt' => [
78 3
                static::TYPE => '\DateTime',
79 3
                static::DECORATOR => '\Commercetools\Core\Model\Common\DateTimeDecorator'
80
            ],
81 3
            'productType' => [static::TYPE => '\Commercetools\Core\Model\ProductType\ProductTypeReference'],
82 3
            'name' => [static::TYPE => '\Commercetools\Core\Model\Common\LocalizedString'],
83 3
            'description' => [static::TYPE => '\Commercetools\Core\Model\Common\LocalizedString'],
84 3
            'slug' => [static::TYPE => '\Commercetools\Core\Model\Common\LocalizedString'],
85 3
            'categories' => [static::TYPE => '\Commercetools\Core\Model\Category\CategoryReferenceCollection'],
86 3
            'categoryOrderHints' => [static::TYPE => 'array'],
87 3
            'metaTitle' => [static::TYPE => '\Commercetools\Core\Model\Common\LocalizedString'],
88 3
            'metaDescription' => [static::TYPE => '\Commercetools\Core\Model\Common\LocalizedString'],
89 3
            'metaKeywords' => [static::TYPE => '\Commercetools\Core\Model\Common\LocalizedString'],
90 3
            'hasStagedChanges' => [static::TYPE => 'bool'],
91 3
            'published' => [static::TYPE => 'bool'],
92 3
            'masterVariant' => [static::TYPE => '\Commercetools\Core\Model\Product\ProductVariant'],
93 3
            'variants' => [static::TYPE => '\Commercetools\Core\Model\Product\ProductVariantCollection'],
94 3
            'taxCategory' => [static::TYPE => '\Commercetools\Core\Model\TaxCategory\TaxCategoryReference'],
95 3
            'searchKeywords' => [static::TYPE => '\Commercetools\Core\Model\Product\LocalizedSearchKeywords'],
96 3
            'reviewRatingStatistics' => [static::TYPE => '\Commercetools\Core\Model\Review\ReviewRatingStatistics'],
97 3
            'state' => [static::TYPE => '\Commercetools\Core\Model\State\StateReference'],
98
        ];
99
    }
100
101
    /**
102
     * @param $variantId
103
     * @return ProductVariant|null
104
     */
105
    public function getVariantById($variantId)
106
    {
107
        if ($variantId == $this->getMasterVariant()->getId()) {
108
            return $this->getMasterVariant();
109
        }
110
111
        return $this->getVariants()->getById($variantId);
112
    }
113
114
    /**
115
     * @param $sku
116
     * @return ProductVariant
117
     */
118
    public function getVariantBySku($sku)
119
    {
120
        if ($sku == $this->getMasterVariant()->getSku()) {
121
            return $this->getMasterVariant();
122
        }
123
124
        return $this->getVariants()->getBySku($sku);
125
    }
126
127
    public function getAllVariants()
128
    {
129
        $variants = $this->getRaw('variants', []);
130
        array_unshift($variants, $this->getRaw('masterVariant'));
131
        return ProductVariantCollection::fromArray($variants, $this->getContextCallback());
132
    }
133
134
    public function getReferenceIdentifier()
135
    {
136
        return $this->getId();
137
    }
138
139
    /**
140
     * @return ProductReference
141
     */
142
    public function getReference()
143
    {
144
        $reference = ProductReference::ofId($this->getReferenceIdentifier(), $this->getContextCallback());
145
        return $reference;
146
    }
147
}
148