Completed
Push — master ( abb6bd...6375fb )
by Bukashk0zzz
01:10
created

AbstractOffer::setCpa()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Bukashk0zzzYmlGenerator
5
 *
6
 * (c) Denis Golubovskiy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Bukashk0zzz\YmlGenerator\Model\Offer;
13
14
use Bukashk0zzz\YmlGenerator\Model\Delivery;
15
16
/**
17
 * Abstract Class Offer
18
 */
19
abstract class AbstractOffer implements OfferInterface
20
{
21
    /**
22
     * @var string
23
     */
24
    private $id;
25
26
    /**
27
     * @var bool
28
     */
29
    private $available;
30
31
    /**
32
     * @var string
33
     */
34
    private $url;
35
36
    /**
37
     * @var float
38
     */
39
    private $price;
40
41
    /**
42
     * @var float
43
     */
44
    private $oldPrice;
45
46
    /**
47
     * @var string
48
     */
49
    private $currencyId;
50
51
    /**
52
     * @var int
53
     */
54
    private $categoryId;
55
56
    /**
57
     * @var array
58
     */
59
    private $categoriesId = [];
60
61
    /**
62
     * @var string
63
     */
64
    private $name;
65
66
    /**
67
     * @var string
68
     */
69
    private $marketCategory;
70
71
    /**
72
     * @var bool
73
     */
74
    private $adult;
75
76
    /**
77
     * @var string
78
     */
79
    private $salesNotes;
80
81
    /**
82
     * @var bool
83
     */
84
    private $manufacturerWarranty;
85
86
    /**
87
     * @var bool
88
     */
89
    private $pickup;
90
91
    /**
92
     * @var bool
93
     */
94
    private $downloadable;
95
96
    /**
97
     * @var bool
98
     */
99
    private $delivery;
100
101
    /**
102
     * @var float
103
     */
104
    private $localDeliveryCost;
105
106
    /**
107
     * @var array
108
     */
109
    private $deliveryOptions = [];
110
111
    /**
112
     * @var string
113
     */
114
    private $description;
115
116
    /**
117
     * @var string
118
     */
119
    private $countryOfOrigin;
120
121
    /**
122
     * @var string
123
     */
124
    private $weight;
125
126
    /**
127
     * @var int
128
     */
129
    private $cpa;
130
131
    /**
132
     * @var string[]
133
     */
134
    private $barcodes;
135
136
    /**
137
     * @var array
138
     */
139
    private $pictures = [];
140
141
    /**
142
     * @var array
143
     */
144
    private $params = [];
145
146
    /**
147
     * @var bool
148
     */
149
    private $store;
150
151
    /**
152
     * Array of custom elements (element types are keys) of arrays of element values
153
     * There may be multiple elements of the same type
154
     *
155
     * @var array[]
156
     */
157
    private $customElements;
158
159
    /**
160
     * @var OfferCondition
161
     */
162
    private $condition;
163
164
    /**
165
     * @return array
166
     */
167
    public function toArray()
168
    {
169
        return \array_merge($this->getHeaderOptions(), $this->getOptions(), $this->getFooterOptions());
170
    }
171
172
    /**
173
     * @return string
174
     */
175
    public function getId()
176
    {
177
        return $this->id;
178
    }
179
180
    /**
181
     * @param string $id
182
     *
183
     * @return $this
184
     */
185
    public function setId($id)
186
    {
187
        $this->id = $id;
188
189
        return $this;
190
    }
191
192
    /**
193
     * @return bool
194
     */
195
    public function isAvailable()
196
    {
197
        return $this->available;
198
    }
199
200
    /**
201
     * @param bool $available
202
     *
203
     * @return $this
204
     */
205
    public function setAvailable($available)
206
    {
207
        $this->available = $available;
208
209
        return $this;
210
    }
211
212
    /**
213
     * @return string
214
     */
215
    public function getUrl()
216
    {
217
        return $this->url;
218
    }
219
220
    /**
221
     * @param string $url
222
     *
223
     * @return $this
224
     */
225
    public function setUrl($url)
226
    {
227
        $this->url = $url;
228
229
        return $this;
230
    }
231
232
    /**
233
     * @return float
234
     */
235
    public function getPrice()
236
    {
237
        return $this->price;
238
    }
239
240
    /**
241
     * @param float $price
242
     *
243
     * @return $this
244
     */
245
    public function setPrice($price)
246
    {
247
        $this->price = $price;
248
249
        return $this;
250
    }
251
252
    /**
253
     * @return float
254
     */
255
    public function getOldPrice()
256
    {
257
        return $this->oldPrice;
258
    }
259
260
    /**
261
     * @param float $oldPrice
262
     *
263
     * @return $this
264
     */
265
    public function setOldPrice($oldPrice)
266
    {
267
        $this->oldPrice = $oldPrice;
268
269
        return $this;
270
    }
271
272
    /**
273
     * @return string
274
     */
275
    public function getCurrencyId()
276
    {
277
        return $this->currencyId;
278
    }
279
280
    /**
281
     * @param string $currencyId
282
     *
283
     * @return $this
284
     */
285
    public function setCurrencyId($currencyId)
286
    {
287
        $this->currencyId = $currencyId;
288
289
        return $this;
290
    }
291
292
    /**
293
     * @return int
294
     */
295
    public function getCategoryId()
296
    {
297
        return $this->categoryId;
298
    }
299
300
    /**
301
     * @param int $categoryId
302
     *
303
     * @return $this
304
     */
305
    public function setCategoryId($categoryId)
306
    {
307
        $this->categoryId = $categoryId;
308
309
        return $this;
310
    }
311
312
    /**
313
     * @return array
314
     */
315
    public function getCategoriesId()
316
    {
317
        return $this->categoriesId;
318
    }
319
320
    /**
321
     * @param array $categoriesId
322
     *
323
     * @return $this
324
     */
325
    public function setCategoriesId(array $categoriesId)
326
    {
327
        $this->categoriesId = $categoriesId;
328
329
        return $this;
330
    }
331
332
    /**
333
     * @return string
334
     */
335
    public function getName()
336
    {
337
        return $this->name;
338
    }
339
340
    /**
341
     * @param string $name
342
     *
343
     * @return $this
344
     */
345
    public function setName($name)
346
    {
347
        $this->name = $name;
348
349
        return $this;
350
    }
351
352
    /**
353
     * @return string
354
     */
355
    public function getMarketCategory()
356
    {
357
        return $this->marketCategory;
358
    }
359
360
    /**
361
     * @param string $marketCategory
362
     *
363
     * @return $this
364
     */
365
    public function setMarketCategory($marketCategory)
366
    {
367
        $this->marketCategory = $marketCategory;
368
369
        return $this;
370
    }
371
372
    /**
373
     * @return bool
374
     */
375
    public function isAdult()
376
    {
377
        return $this->adult;
378
    }
379
380
    /**
381
     * @param bool $adult
382
     *
383
     * @return $this
384
     */
385
    public function setAdult($adult)
386
    {
387
        $this->adult = $adult;
388
389
        return $this;
390
    }
391
392
    /**
393
     * @return string
394
     */
395
    public function getSalesNotes()
396
    {
397
        return $this->salesNotes;
398
    }
399
400
    /**
401
     * @param string $salesNotes
402
     *
403
     * @return $this
404
     */
405
    public function setSalesNotes($salesNotes)
406
    {
407
        $this->salesNotes = $salesNotes;
408
409
        return $this;
410
    }
411
412
    /**
413
     * @return bool
414
     */
415
    public function isManufacturerWarranty()
416
    {
417
        return $this->manufacturerWarranty;
418
    }
419
420
    /**
421
     * @param bool $manufacturerWarranty
422
     *
423
     * @return $this
424
     */
425
    public function setManufacturerWarranty($manufacturerWarranty)
426
    {
427
        $this->manufacturerWarranty = $manufacturerWarranty;
428
429
        return $this;
430
    }
431
432
    /**
433
     * @return bool
434
     */
435
    public function isPickup()
436
    {
437
        return $this->pickup;
438
    }
439
440
    /**
441
     * @param bool $pickup
442
     *
443
     * @return $this
444
     */
445
    public function setPickup($pickup)
446
    {
447
        $this->pickup = $pickup;
448
449
        return $this;
450
    }
451
452
    /**
453
     * @return bool
454
     */
455
    public function isDownloadable()
456
    {
457
        return $this->downloadable;
458
    }
459
460
    /**
461
     * @param bool $downloadable
462
     *
463
     * @return $this
464
     */
465
    public function setDownloadable($downloadable)
466
    {
467
        $this->downloadable = $downloadable;
468
469
        return $this;
470
    }
471
472
    /**
473
     * @return bool
474
     */
475
    public function isDelivery()
476
    {
477
        return $this->delivery;
478
    }
479
480
    /**
481
     * @param bool $delivery
482
     *
483
     * @return $this
484
     */
485
    public function setDelivery($delivery)
486
    {
487
        $this->delivery = $delivery;
488
489
        return $this;
490
    }
491
492
    /**
493
     * @return array
494
     */
495
    public function getDeliveryOptions()
496
    {
497
        return $this->deliveryOptions;
498
    }
499
500
    /**
501
     * @param Delivery $option
502
     *
503
     * @return $this
504
     */
505
    public function addDeliveryOption(Delivery $option)
506
    {
507
        $this->deliveryOptions[] = $option;
508
509
        return $this;
510
    }
511
512
    /**
513
     * @param bool $store
514
     *
515
     * @return $this
516
     */
517
    public function setStore($store)
518
    {
519
        $this->store = $store;
520
521
        return $this;
522
    }
523
524
    /**
525
     * @return bool
526
     */
527
    public function isStore()
528
    {
529
        return $this->store;
530
    }
531
532
    /**
533
     * @return float
534
     */
535
    public function getLocalDeliveryCost()
536
    {
537
        return $this->localDeliveryCost;
538
    }
539
540
    /**
541
     * @param float $localDeliveryCost
542
     *
543
     * @return $this
544
     */
545
    public function setLocalDeliveryCost($localDeliveryCost)
546
    {
547
        $this->localDeliveryCost = $localDeliveryCost;
548
549
        return $this;
550
    }
551
552
    /**
553
     * @return string
554
     */
555
    public function getDescription()
556
    {
557
        return $this->description;
558
    }
559
560
    /**
561
     * @param string $description
562
     *
563
     * @return $this
564
     */
565
    public function setDescription($description)
566
    {
567
        $this->description = $description;
568
569
        return $this;
570
    }
571
572
    /**
573
     * @return string
574
     */
575
    public function getCountryOfOrigin()
576
    {
577
        return $this->countryOfOrigin;
578
    }
579
580
    /**
581
     * @param string $countryOfOrigin
582
     *
583
     * @return $this
584
     */
585
    public function setCountryOfOrigin($countryOfOrigin)
586
    {
587
        $this->countryOfOrigin = $countryOfOrigin;
588
589
        return $this;
590
    }
591
592
    /**
593
     * @return string
594
     */
595
    public function getWeight()
596
    {
597
        return $this->weight;
598
    }
599
600
    /**
601
     * @param string $weight
602
     *
603
     * @return $this
604
     */
605
    public function setWeight($weight)
606
    {
607
        $this->weight = $weight;
608
609
        return $this;
610
    }
611
612
    /**
613
     * @return int
614
     */
615
    public function getCpa()
616
    {
617
        return $this->cpa;
618
    }
619
620
    /**
621
     * @param int $cpa
622
     *
623
     * @return $this
624
     */
625
    public function setCpa($cpa)
626
    {
627
        $this->cpa = $cpa;
628
629
        return $this;
630
    }
631
632
    /**
633
     * @return array
634
     */
635
    public function getParams()
636
    {
637
        return $this->params;
638
    }
639
640
    /**
641
     * @param OfferParam $param
642
     *
643
     * @return $this
644
     */
645
    public function addParam(OfferParam $param)
646
    {
647
        $this->params[] = $param;
648
649
        return $this;
650
    }
651
652
    /**
653
     * Add picture
654
     *
655
     * @param string $url
656
     *
657
     * @return $this
658
     */
659
    public function addPicture($url)
660
    {
661
        if (\count($this->pictures) < 10) {
662
            $this->pictures[] = $url;
663
        }
664
665
        return $this;
666
    }
667
668
    /**
669
     * Set pictures
670
     *
671
     * @param array $pictures
672
     *
673
     * @return $this
674
     */
675
    public function setPictures(array $pictures)
676
    {
677
        $this->pictures = $pictures;
678
679
        return $this;
680
    }
681
682
    /**
683
     * Get picture list
684
     *
685
     * @return array
686
     */
687
    public function getPictures()
688
    {
689
        return $this->pictures;
690
    }
691
692
    /**
693
     * Get list of barcodes of the offer
694
     *
695
     * @return string[]
696
     */
697
    public function getBarcodes()
698
    {
699
        return $this->barcodes;
700
    }
701
702
    /**
703
     * Set list of barcodes for that offer
704
     *
705
     * @param string[] $barcodes
706
     *
707
     * @return $this
708
     */
709
    public function setBarcodes(array $barcodes = [])
710
    {
711
        $this->barcodes = $barcodes;
712
713
        return $this;
714
    }
715
716
    /**
717
     * Add one barcode to the collection of barcodes of this offer
718
     *
719
     * @param string $barcode
720
     *
721
     * @return AbstractOffer
722
     */
723
    public function addBarcode($barcode)
724
    {
725
        $this->barcodes[] = $barcode;
726
727
        return $this;
728
    }
729
730
    /**
731
     * Sets list of custom elements
732
     *
733
     * @param array $customElements Array (keys are element types) of arrays (element values)
734
     *
735
     * @return $this
736
     */
737
    public function setCustomElements(array $customElements = [])
738
    {
739
        $this->customElements = $customElements;
740
741
        return $this;
742
    }
743
744
    /**
745
     * Add a custom element with given type and value
746
     * Multiple elements of the same type are supported
747
     *
748
     * @param string $elementType
749
     * @param mixed  $value
750
     *
751
     * @return $this
752
     */
753
    public function addCustomElement($elementType, $value)
754
    {
755
        if ($value !== null) {
756
            // Add value to the list of values of the given element type creating array when needed
757
            $this->customElements[$elementType][] = $value;
758
        }
759
760
        return $this;
761
    }
762
763
    /**
764
     * Returns a list of custom elements
765
     * Always returns an array even if no custom elements were added
766
     *
767
     * @return array
768
     */
769
    public function getCustomElements()
770
    {
771
        return $this->customElements ?: [];
772
    }
773
774
    /**
775
     * Returns a list of values for the specified custom element type
776
     * Always returns an array
777
     *
778
     * @param string $elementType
779
     *
780
     * @return array
781
     */
782
    public function getCustomElementByType($elementType)
783
    {
784
        // TODO: Use ?? operator when support for PHP 5.6 is no longer needed
785
        if (isset($this->customElements[$elementType])) {
786
            return $this->customElements[$elementType];
787
        }
788
789
        return [];
790
    }
791
792
    /**
793
     * @return OfferCondition
794
     */
795
    public function getCondition()
796
    {
797
        return $this->condition;
798
    }
799
800
    /**
801
     * @param OfferCondition $condition
802
     *
803
     * @return $this
804
     */
805
    public function addCondition(OfferCondition $condition)
806
    {
807
        $this->condition = $condition;
808
809
        return $this;
810
    }
811
812
    /**
813
     * @return array
814
     */
815
    abstract protected function getOptions();
816
817
    /**
818
     * @return array
819
     */
820
    private function getHeaderOptions()
821
    {
822
        return [
823
                'url' => $this->getUrl(),
824
                'price' => $this->getPrice(),
825
                'oldprice' => $this->getOldPrice(),
826
                'currencyId' => $this->getCurrencyId(),
827
                'categoryId' => \array_merge(
828
                    [$this->getCategoryId()],
829
                    $this->getCategoriesId()
830
                ),
831
                'market_category' => $this->getMarketCategory(),
832
                'picture' => $this->getPictures(),
833
                'pickup' => $this->isPickup(),
834
                'store' => $this->isStore(),
835
                'delivery' => $this->isDelivery(),
836
                'local_delivery_cost' => $this->getLocalDeliveryCost(),
837
                'weight' => $this->getWeight(),
838
                'name' => $this->getName(),
839
            ] + $this->getCustomElements();
840
    }
841
842
    /**
843
     * @return array
844
     */
845
    private function getFooterOptions()
846
    {
847
        return [
848
            'description' => $this->getDescription(),
849
            'sales_notes' => $this->getSalesNotes(),
850
            'manufacturer_warranty' => $this->isManufacturerWarranty(),
851
            'country_of_origin' => $this->getCountryOfOrigin(),
852
            'downloadable' => $this->isDownloadable(),
853
            'adult' => $this->isAdult(),
854
            'cpa' => $this->getCpa(),
855
            'barcode' => $this->getBarcodes(),
856
        ];
857
    }
858
}
859