Completed
Push — master ( 517bde...47329e )
by Bukashk0zzz
01:12
created

AbstractOffer::setAutoDiscount()   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
     * @var bool
153
     */
154
    private $autoDiscount;
155
156
    /**
157
     * Array of custom elements (element types are keys) of arrays of element values
158
     * There may be multiple elements of the same type
159
     *
160
     * @var array[]
161
     */
162
    private $customElements;
163
164
    /**
165
     * @var OfferCondition
166
     */
167
    private $condition;
168
169
    /**
170
     * @return array
171
     */
172
    public function toArray()
173
    {
174
        return \array_merge($this->getHeaderOptions(), $this->getOptions(), $this->getFooterOptions());
175
    }
176
177
    /**
178
     * @return string
179
     */
180
    public function getId()
181
    {
182
        return $this->id;
183
    }
184
185
    /**
186
     * @param string $id
187
     *
188
     * @return $this
189
     */
190
    public function setId($id)
191
    {
192
        $this->id = $id;
193
194
        return $this;
195
    }
196
197
    /**
198
     * @return bool
199
     */
200
    public function isAvailable()
201
    {
202
        return $this->available;
203
    }
204
205
    /**
206
     * @param bool $available
207
     *
208
     * @return $this
209
     */
210
    public function setAvailable($available)
211
    {
212
        $this->available = $available;
213
214
        return $this;
215
    }
216
217
    /**
218
     * @return string
219
     */
220
    public function getUrl()
221
    {
222
        return $this->url;
223
    }
224
225
    /**
226
     * @param string $url
227
     *
228
     * @return $this
229
     */
230
    public function setUrl($url)
231
    {
232
        $this->url = $url;
233
234
        return $this;
235
    }
236
237
    /**
238
     * @return float
239
     */
240
    public function getPrice()
241
    {
242
        return $this->price;
243
    }
244
245
    /**
246
     * @param float $price
247
     *
248
     * @return $this
249
     */
250
    public function setPrice($price)
251
    {
252
        $this->price = $price;
253
254
        return $this;
255
    }
256
257
    /**
258
     * @return float
259
     */
260
    public function getOldPrice()
261
    {
262
        return $this->oldPrice;
263
    }
264
265
    /**
266
     * @param float $oldPrice
267
     *
268
     * @return $this
269
     */
270
    public function setOldPrice($oldPrice)
271
    {
272
        $this->oldPrice = $oldPrice;
273
274
        return $this;
275
    }
276
277
    /**
278
     * @return string
279
     */
280
    public function getCurrencyId()
281
    {
282
        return $this->currencyId;
283
    }
284
285
    /**
286
     * @param string $currencyId
287
     *
288
     * @return $this
289
     */
290
    public function setCurrencyId($currencyId)
291
    {
292
        $this->currencyId = $currencyId;
293
294
        return $this;
295
    }
296
297
    /**
298
     * @return int
299
     */
300
    public function getCategoryId()
301
    {
302
        return $this->categoryId;
303
    }
304
305
    /**
306
     * @param int $categoryId
307
     *
308
     * @return $this
309
     */
310
    public function setCategoryId($categoryId)
311
    {
312
        $this->categoryId = $categoryId;
313
314
        return $this;
315
    }
316
317
    /**
318
     * @return array
319
     */
320
    public function getCategoriesId()
321
    {
322
        return $this->categoriesId;
323
    }
324
325
    /**
326
     * @param array $categoriesId
327
     *
328
     * @return $this
329
     */
330
    public function setCategoriesId(array $categoriesId)
331
    {
332
        $this->categoriesId = $categoriesId;
333
334
        return $this;
335
    }
336
337
    /**
338
     * @return string
339
     */
340
    public function getName()
341
    {
342
        return $this->name;
343
    }
344
345
    /**
346
     * @param string $name
347
     *
348
     * @return $this
349
     */
350
    public function setName($name)
351
    {
352
        $this->name = $name;
353
354
        return $this;
355
    }
356
357
    /**
358
     * @return string
359
     */
360
    public function getMarketCategory()
361
    {
362
        return $this->marketCategory;
363
    }
364
365
    /**
366
     * @param string $marketCategory
367
     *
368
     * @return $this
369
     */
370
    public function setMarketCategory($marketCategory)
371
    {
372
        $this->marketCategory = $marketCategory;
373
374
        return $this;
375
    }
376
377
    /**
378
     * @return bool
379
     */
380
    public function isAdult()
381
    {
382
        return $this->adult;
383
    }
384
385
    /**
386
     * @param bool $adult
387
     *
388
     * @return $this
389
     */
390
    public function setAdult($adult)
391
    {
392
        $this->adult = $adult;
393
394
        return $this;
395
    }
396
397
    /**
398
     * @return string
399
     */
400
    public function getSalesNotes()
401
    {
402
        return $this->salesNotes;
403
    }
404
405
    /**
406
     * @param string $salesNotes
407
     *
408
     * @return $this
409
     */
410
    public function setSalesNotes($salesNotes)
411
    {
412
        $this->salesNotes = $salesNotes;
413
414
        return $this;
415
    }
416
417
    /**
418
     * @return bool
419
     */
420
    public function isManufacturerWarranty()
421
    {
422
        return $this->manufacturerWarranty;
423
    }
424
425
    /**
426
     * @param bool $manufacturerWarranty
427
     *
428
     * @return $this
429
     */
430
    public function setManufacturerWarranty($manufacturerWarranty)
431
    {
432
        $this->manufacturerWarranty = $manufacturerWarranty;
433
434
        return $this;
435
    }
436
437
    /**
438
     * @return bool
439
     */
440
    public function isPickup()
441
    {
442
        return $this->pickup;
443
    }
444
445
    /**
446
     * @param bool $pickup
447
     *
448
     * @return $this
449
     */
450
    public function setPickup($pickup)
451
    {
452
        $this->pickup = $pickup;
453
454
        return $this;
455
    }
456
457
    /**
458
     * @return bool
459
     */
460
    public function isDownloadable()
461
    {
462
        return $this->downloadable;
463
    }
464
465
    /**
466
     * @param bool $downloadable
467
     *
468
     * @return $this
469
     */
470
    public function setDownloadable($downloadable)
471
    {
472
        $this->downloadable = $downloadable;
473
474
        return $this;
475
    }
476
477
    /**
478
     * @return bool
479
     */
480
    public function isDelivery()
481
    {
482
        return $this->delivery;
483
    }
484
485
    /**
486
     * @param bool $delivery
487
     *
488
     * @return $this
489
     */
490
    public function setDelivery($delivery)
491
    {
492
        $this->delivery = $delivery;
493
494
        return $this;
495
    }
496
497
    /**
498
     * @return array
499
     */
500
    public function getDeliveryOptions()
501
    {
502
        return $this->deliveryOptions;
503
    }
504
505
    /**
506
     * @param Delivery $option
507
     *
508
     * @return $this
509
     */
510
    public function addDeliveryOption(Delivery $option)
511
    {
512
        $this->deliveryOptions[] = $option;
513
514
        return $this;
515
    }
516
517
    /**
518
     * @param bool $store
519
     *
520
     * @return $this
521
     */
522
    public function setStore($store)
523
    {
524
        $this->store = $store;
525
526
        return $this;
527
    }
528
529
    /**
530
     * @return bool
531
     */
532
    public function isStore()
533
    {
534
        return $this->store;
535
    }
536
537
    /**
538
     * @return float
539
     */
540
    public function getLocalDeliveryCost()
541
    {
542
        return $this->localDeliveryCost;
543
    }
544
545
    /**
546
     * @param float $localDeliveryCost
547
     *
548
     * @return $this
549
     */
550
    public function setLocalDeliveryCost($localDeliveryCost)
551
    {
552
        $this->localDeliveryCost = $localDeliveryCost;
553
554
        return $this;
555
    }
556
557
    /**
558
     * @return string
559
     */
560
    public function getDescription()
561
    {
562
        return $this->description;
563
    }
564
565
    /**
566
     * @param string $description
567
     *
568
     * @return $this
569
     */
570
    public function setDescription($description)
571
    {
572
        $this->description = $description;
573
574
        return $this;
575
    }
576
577
    /**
578
     * @return string
579
     */
580
    public function getCountryOfOrigin()
581
    {
582
        return $this->countryOfOrigin;
583
    }
584
585
    /**
586
     * @param string $countryOfOrigin
587
     *
588
     * @return $this
589
     */
590
    public function setCountryOfOrigin($countryOfOrigin)
591
    {
592
        $this->countryOfOrigin = $countryOfOrigin;
593
594
        return $this;
595
    }
596
597
    /**
598
     * @return string
599
     */
600
    public function getWeight()
601
    {
602
        return $this->weight;
603
    }
604
605
    /**
606
     * @param string $weight
607
     *
608
     * @return $this
609
     */
610
    public function setWeight($weight)
611
    {
612
        $this->weight = $weight;
613
614
        return $this;
615
    }
616
617
    /**
618
     * @return int
619
     */
620
    public function getCpa()
621
    {
622
        return $this->cpa;
623
    }
624
625
    /**
626
     * @param int $cpa
627
     *
628
     * @return $this
629
     */
630
    public function setCpa($cpa)
631
    {
632
        $this->cpa = $cpa;
633
634
        return $this;
635
    }
636
637
    /**
638
     * @return array
639
     */
640
    public function getParams()
641
    {
642
        return $this->params;
643
    }
644
645
    /**
646
     * @param OfferParam $param
647
     *
648
     * @return $this
649
     */
650
    public function addParam(OfferParam $param)
651
    {
652
        $this->params[] = $param;
653
654
        return $this;
655
    }
656
657
    /**
658
     * Add picture
659
     *
660
     * @param string $url
661
     *
662
     * @return $this
663
     */
664
    public function addPicture($url)
665
    {
666
        if (\count($this->pictures) < 10) {
667
            $this->pictures[] = $url;
668
        }
669
670
        return $this;
671
    }
672
673
    /**
674
     * Set pictures
675
     *
676
     * @param array $pictures
677
     *
678
     * @return $this
679
     */
680
    public function setPictures(array $pictures)
681
    {
682
        $this->pictures = $pictures;
683
684
        return $this;
685
    }
686
687
    /**
688
     * Get picture list
689
     *
690
     * @return array
691
     */
692
    public function getPictures()
693
    {
694
        return $this->pictures;
695
    }
696
697
    /**
698
     * Get list of barcodes of the offer
699
     *
700
     * @return string[]
701
     */
702
    public function getBarcodes()
703
    {
704
        return $this->barcodes;
705
    }
706
707
    /**
708
     * Set list of barcodes for that offer
709
     *
710
     * @param string[] $barcodes
711
     *
712
     * @return $this
713
     */
714
    public function setBarcodes(array $barcodes = [])
715
    {
716
        $this->barcodes = $barcodes;
717
718
        return $this;
719
    }
720
721
    /**
722
     * Add one barcode to the collection of barcodes of this offer
723
     *
724
     * @param string $barcode
725
     *
726
     * @return $this
727
     */
728
    public function addBarcode($barcode)
729
    {
730
        $this->barcodes[] = $barcode;
731
732
        return $this;
733
    }
734
735
    /**
736
     * Sets list of custom elements
737
     *
738
     * @param array $customElements Array (keys are element types) of arrays (element values)
739
     *
740
     * @return $this
741
     */
742
    public function setCustomElements(array $customElements = [])
743
    {
744
        $this->customElements = $customElements;
745
746
        return $this;
747
    }
748
749
    /**
750
     * Add a custom element with given type and value
751
     * Multiple elements of the same type are supported
752
     *
753
     * @param string $elementType
754
     * @param mixed  $value
755
     *
756
     * @return $this
757
     */
758
    public function addCustomElement($elementType, $value)
759
    {
760
        if ($value !== null) {
761
            // Add value to the list of values of the given element type creating array when needed
762
            $this->customElements[$elementType][] = $value;
763
        }
764
765
        return $this;
766
    }
767
768
    /**
769
     * Returns a list of custom elements
770
     * Always returns an array even if no custom elements were added
771
     *
772
     * @return array
773
     */
774
    public function getCustomElements()
775
    {
776
        return $this->customElements ?: [];
777
    }
778
779
    /**
780
     * Returns a list of values for the specified custom element type
781
     * Always returns an array
782
     *
783
     * @param string $elementType
784
     *
785
     * @return array
786
     */
787
    public function getCustomElementByType($elementType)
788
    {
789
        // TODO: Use ?? operator when support for PHP 5.6 is no longer needed
790
        if (isset($this->customElements[$elementType])) {
791
            return $this->customElements[$elementType];
792
        }
793
794
        return [];
795
    }
796
797
    /**
798
     * @return OfferCondition
799
     */
800
    public function getCondition()
801
    {
802
        return $this->condition;
803
    }
804
805
    /**
806
     * @param OfferCondition $condition
807
     *
808
     * @return $this
809
     */
810
    public function addCondition(OfferCondition $condition)
811
    {
812
        $this->condition = $condition;
813
814
        return $this;
815
    }
816
817
    /**
818
     * @return bool
819
     */
820
    public function getAutoDiscount()
821
    {
822
        return $this->autoDiscount;
823
    }
824
825
    /**
826
     * @param bool $autoDiscount
827
     *
828
     * @return $this
829
     */
830
    public function setAutoDiscount($autoDiscount)
831
    {
832
        $this->autoDiscount = $autoDiscount;
833
834
        return $this;
835
    }
836
837
    /**
838
     * @return array
839
     */
840
    abstract protected function getOptions();
841
842
    /**
843
     * @return array
844
     */
845
    private function getHeaderOptions()
846
    {
847
        return [
848
                'url' => $this->getUrl(),
849
                'price' => $this->getPrice(),
850
                'oldprice' => $this->getOldPrice(),
851
                'currencyId' => $this->getCurrencyId(),
852
                'categoryId' => \array_merge(
853
                    [$this->getCategoryId()],
854
                    $this->getCategoriesId()
855
                ),
856
                'market_category' => $this->getMarketCategory(),
857
                'picture' => $this->getPictures(),
858
                'pickup' => $this->isPickup(),
859
                'store' => $this->isStore(),
860
                'delivery' => $this->isDelivery(),
861
                'local_delivery_cost' => $this->getLocalDeliveryCost(),
862
                'weight' => $this->getWeight(),
863
                'name' => $this->getName(),
864
                'enable_auto_discounts' => $this->getAutoDiscount(),
865
            ] + $this->getCustomElements();
866
    }
867
868
    /**
869
     * @return array
870
     */
871
    private function getFooterOptions()
872
    {
873
        return [
874
            'description' => $this->getDescription(),
875
            'sales_notes' => $this->getSalesNotes(),
876
            'manufacturer_warranty' => $this->isManufacturerWarranty(),
877
            'country_of_origin' => $this->getCountryOfOrigin(),
878
            'downloadable' => $this->isDownloadable(),
879
            'adult' => $this->isAdult(),
880
            'cpa' => $this->getCpa(),
881
            'barcode' => $this->getBarcodes(),
882
        ];
883
    }
884
}
885