1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Pricing;
use Magento\Framework\Pricing\Price\Factory as PriceFactory;
use Magento\Framework\Pricing\Price\PriceInterface;
/**
* Composite price model
*/
class PriceComposite
{
/**
* @var PriceFactory
*/
protected $priceFactory;
/**
* @var array
*/
protected $metadata;
/**
* @param PriceFactory $priceFactory
* @param array $metadata
*/
public function __construct(PriceFactory $priceFactory, array $metadata = [])
{
$this->priceFactory = $priceFactory;
$this->metadata = $metadata;
}
/**
* @return array
*/
public function getPriceCodes()
{
return array_keys($this->metadata);
}
/**
* Returns metadata for prices
*
* @return array
*/
public function getMetadata()
{
return $this->metadata;
}
/**
* @param SaleableInterface $salableItem
* @param string $priceCode
* @param float $quantity
* @return PriceInterface
* @throws \InvalidArgumentException
*/
public function createPriceObject(SaleableInterface $salableItem, $priceCode, $quantity)
{
if (!isset($this->metadata[$priceCode])) {
throw new \InvalidArgumentException($priceCode . ' is not registered in prices list');
}
$className = $this->metadata[$priceCode]['class'];
return $this->priceFactory->create($salableItem, $className, $quantity);
}
}