TemplatePricePresenter::renderPrice()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 0
cts 18
cp 0
rs 8.8177
c 0
b 0
f 0
cc 6
nc 8
nop 1
crap 42
1
<?php
2
/**
3
 * Finance module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-finance
6
 * @package   hipanel-module-finance
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\grid\presenters\price;
12
13
use hipanel\helpers\StringHelper;
14
use hipanel\modules\finance\models\Price;
15
use Yii;
16
use yii\base\InvalidConfigException;
17
use yii\helpers\Html;
18
19
/**
20
 * Class TemplatePricePresenter.
21
 *
22
 * @author Dmytro Naumenko <[email protected]>
23
 */
24
class TemplatePricePresenter extends PricePresenter
25
{
26
    /**
27
     * @param \hipanel\modules\finance\models\TemplatePrice $price
28
     * @return string
29
     */
30
    public function renderPrice(Price $price): string
31
    {
32
        $formatter = Yii::$app->formatter;
33
34
        $unit = '';
35
        if ($price->getUnitLabel()) {
36
            $unit = ' ' . Yii::t('hipanel:finance', 'per {unit}', ['unit' => $price->getUnitLabel()]);
37
        }
38
39
        if (StringHelper::startsWith($price->type, 'referral')) {
40
            $result = [
41
                Html::tag('strong', $price->rate . '%'),
0 ignored issues
show
Documentation introduced by
The property rate does not exist on object<hipanel\modules\finance\models\Price>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
42
            ];
43
        } else {
44
            $result = [
45
                Html::tag('strong', $formatter->asCurrency($price->price, $price->currency) . $unit),
46
            ];
47
        }
48
        if ($price->subprices) {
0 ignored issues
show
Documentation introduced by
The property subprices does not exist on object<hipanel\modules\finance\models\Price>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
49
            foreach ($price->subprices as $currencyCode => $amount) {
0 ignored issues
show
Documentation introduced by
The property subprices does not exist on object<hipanel\modules\finance\models\Price>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
50
                try {
51
                    $result[] = $formatter->asCurrency($amount, $currencyCode);
52
                } catch (InvalidConfigException $e) {
53
                    $result[] = $amount . ' ' . $currencyCode;
54
                }
55
            }
56
        }
57
58
        return implode('&nbsp;&mdash;&nbsp;', $result);
59
    }
60
}
61