Wishlist::setItems()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/*
4
 * (c) Soeren Martius
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace SoerenMartius\Component\Wishlist\Model;
11
12
use Doctrine\Common\Collections\{ ArrayCollection, Collection };
13
14
/**
15
 * @author Soeren Martius <[email protected]>
16
 */
17
class Wishlist implements
18
    WishlistInterface,
19
    Timestampable
20
{
21
    use TimestampableTrait;
22
23
    /**
24
     * @var mixed
25
     */
26
    protected $id;
27
28
    /**
29
     * @var string
30
     */
31
    protected $sharingCode;
32
33
    /**
34
     * @var $items Collection|Item[]
35
     */
36
    protected $items;
37
38
    /**
39
     * Wishlist constructor.
40
     */
41
    public function __construct()
42
    {
43
        $this->items       = new ArrayCollection();
44
        $this->sharingCode = substr(base_convert(sha1(uniqid(mt_rand(), true)), 16, 36), 0, 19);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getId()
51
    {
52
        return $this->id;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getItems(): Collection
59
    {
60
        return $this->items;
61
    }
62
63
    /**
64
     * @param Collection|ItemInterface[] $items
65
     *
66
     * @return WishlistInterface
67
     */
68
    public function setItems($items): WishlistInterface
69
    {
70
        $this->items = $items;
71
72
        return $this;
73
    }
74
75
    /**
76
     * @param ItemInterface $item
77
     *
78
     * @return WishlistInterface
79
     */
80
    public function addItem(ItemInterface $item): WishlistInterface
81
    {
82
        $item->setWishlist($this);
83
        $this->items->add($item);
84
85
        return $this;
86
    }
87
88
    /**
89
     * @param ItemInterface $item
90
     *
91
     * @return WishlistInterface
92
     */
93
    public function removeItem(ItemInterface $item): WishlistInterface
94
    {
95
        $this->items->removeElement($item);
96
97
        return $this;
98
    }
99
100
    /**
101
     * @param ItemInterface $item
102
     *
103
     * @return bool
104
     */
105
    public function hasItem(ItemInterface $item): bool
106
    {
107
        return $this->items->contains($item);
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function getSharingCode(): string
114
    {
115
        return $this->sharingCode;
116
    }
117
118
    /**
119
     * @param string $sharingCode
120
     *
121
     * @return WishlistInterface
122
     */
123
    public function setSharingCode($sharingCode): WishlistInterface
124
    {
125
        $this->sharingCode = $sharingCode;
126
127
        return $this;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function getCountedItems(): int
134
    {
135
        return $this->items->count();
136
    }
137
}
138