Completed
Push — develop ( e970db...c13c79 )
by
unknown
07:55
created

ChannelPrices::filter()   C

Complexity

Conditions 7
Paths 11

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 26
rs 6.7272
cc 7
eloc 17
nc 11
nop 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2016 Cross Solution <http://cross-solution.de>
8
 */
9
10
/**
11
 * The price calculations is probably something you wanna do completely different. You can do so by writing
12
 * your own filter.
13
 * This can be done by defining your Factory in the Jobs\config\modules.config.php
14
 *
15
 * 'filters' => [
16
 *   'factories'=> [
17
 *      'Jobs/ChannelPrices'  => 'Your\Factory\Filter\YourPriceCalculationFactory',
18
 *      ...
19
 *     ]
20
 *  ]
21
 *
22
 * You should create a Factory to be able to inject the Options into your Calculation class. Set the name of the
23
 * $filter class in your Factory to you FQN of your Calculation class and implement your calculation.
24
 */
25
namespace Jobs\Filter;
26
27
use Jobs\Options\ChannelOptions;
28
use Jobs\Options\ProviderOptions;
29
use Zend\Filter\Exception;
30
use Zend\Filter\FilterInterface;
31
32
/**
33
 * ${CARET}
34
 * 
35
 * @author Mathias Gelhausen <[email protected]>
36
 * @todo write test 
37
 */
38
class ChannelPrices implements FilterInterface
39
{
40
    protected $providers;
41
42
    public function __construct(ProviderOptions $providers)
43
    {
44
        $this->providers = $providers;
45
    }
46
    /**
47
     * This filter allows you to loop over the selected Channels. Each channel can have three
48
     * prices 'min', 'base', 'list'. The default calculation simply adds a discount of 13,5% if
49
     * more than one channel is selected.
50
     *
51
     * In addition, you'll get a special discount of 100 whatever, if your job will be posted on
52
     * jobs.yawik.org :-)
53
     *
54
     * Returns the result of filtering $value
55
     *
56
     * @param  array $value
57
     *
58
     * @throws Exception\RuntimeException If filtering $value is impossible
59
     * @return mixed
60
     */
61
    public function filter($value = [])
62
    {
63
        $sum = 0;
64
        $amount = 0;
65
        $absoluteDiscount = 0;
66
        if (empty($value)) {
67
            return 0;
68
        }
69
70
        foreach ($value as $channelKey) {
71
            /* @var $channel ChannelOptions */
72
            $channel = $this->providers->getChannel($channelKey);
73
            if ('yawik' == $channelKey) {
74
                $absoluteDiscount = 100;
75
            }
76
            if ($channel instanceof ChannelOptions && $channel->getPrice('base')>0) {
77
                $sum += $channel->getPrice('base');
78
                $amount++;
79
            }
80
        }
81
        $discount=1-($amount-1)*13.5/100;
82
        if ($discount>0) {
83
            $sum= round($sum * $discount, 2);
84
        }
85
        return $sum-$absoluteDiscount;
86
    }
87
}
88