Completed
Push — trunk ( 571dd3...e6b3b6 )
by SuperNova.WS
07:55
created

EconomicHelper::resetResourcesExchange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by Gorlum 01.10.2017 15:07
4
 */
5
6
namespace Meta\Economic;
7
8
use Common\GlobalContainer;
9
10
class EconomicHelper {
11
12
  /**
13
   * @var \classConfig $config
14
   */
15
  protected $config;
16
17
  /**
18
   * @var array[] $resourceExchangeRates
19
   */
20
  protected $resourceExchangeRates = [];
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $resourceExchangeRates exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
21
22
  /**
23
   * EconomicHelper constructor.
24
   *
25
   * @param GlobalContainer $gc
26
   */
27
  public function __construct(GlobalContainer $gc) {
28
//    if ($gc instanceof \classConfig) {
29
//      $this->config = $gc;
30
//    } elseif ($gc instanceof GlobalContainer) {
31
//      $this->config = $gc->config;
32
//    } else {
33
//      $this->config = \classSupernova::$config;
34
//    }
35
36
    $this->config = $gc->config;
0 ignored issues
show
Documentation introduced by
The property config does not exist on object<Common\GlobalContainer>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
37
  }
38
39
  /**
40
   * Resets internal exchange cache
41
   */
42
  public function resetResourcesExchange() {
43
    $this->resourceExchangeRates = [];
44
  }
45
46
  /**
47
   * Gets current exchange rates
48
   *
49
   * @return float[] - [int resourceId] -> [float configuredExchangeRate]
50
   */
51
  public function getResourcesExchange() {
52
    if (empty($this->resourceExchangeRates[UNIT_ANY])) {
53
      $this->resourceExchangeRates[UNIT_ANY] = array(
54
        RES_METAL       => 'rpg_exchange_metal',
55
        RES_CRYSTAL     => 'rpg_exchange_crystal',
56
        RES_DEUTERIUM   => 'rpg_exchange_deuterium',
57
        RES_DARK_MATTER => 'rpg_exchange_darkMatter',
58
      );
59
60
      foreach ($this->resourceExchangeRates[UNIT_ANY] as &$rate) {
61
        ($rate = floatval($this->config->$rate)) <= 0 ? $rate = 1 : false;
62
      }
63
    }
64
65
    return $this->resourceExchangeRates[UNIT_ANY];
66
  }
67
68
  /**
69
   * Get cost of all resources normalized to selected one
70
   *
71
   * Say, get cost of all resources in metal - for main calculations for comparing units between each other
72
   *
73
   * @param int $resourceId
74
   *
75
   * @return float[] - [int resourceId] -> [float ratesNormalizedToResourceCost]
76
   */
77
  public function getResourceExchangeIn($resourceId) {
78
    if(empty($this->resourceExchangeRates[$resourceId])) {
79
      $defaultRates = $this->getResourcesExchange();
80
81
      $this->resourceExchangeRates[$resourceId] = [];
82
      foreach($defaultRates as $defaultResourceId => $defaultRate) {
83
        $this->resourceExchangeRates[$resourceId][$defaultResourceId] = $defaultRate / $defaultRates[$resourceId];
84
      }
85
    }
86
87
    return $this->resourceExchangeRates[$resourceId];
88
  }
89
90
}
91