Passed
Push — trunk ( e5589f...062c60 )
by SuperNova.WS
06:16
created

SkinModel::isImageFileExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 12
rs 10
c 0
b 0
f 0
ccs 0
cts 9
cp 0
crap 6
1
<?php
2
3
use Core\GlobalContainer;
4
5
/**
6
 * Created by Gorlum 23.02.2017 12:20
7
 */
8
class SkinModel {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
  const NO_IMAGE_ID = '_no_image';
10
  const NO_IMAGE_PATH = '/design/images/_no_image.png';
11
12
  /**
13
   * @var GlobalContainer $gc
14
   */
15
  protected $gc;
16
17
  /**
18
   * @var SkinInterface[] $skins
19
   */
20
  // TODO - lazy loading
21
  protected $skins;
22
23
  /**
24
   * @var SkinInterface $activeSkin
25
   */
26
  protected $activeSkin;
27
28
29
  // TODO - remove
30
  public function init() {
31
  }
32
33
34
  /**
35
   * SkinModel constructor.
36
   *
37
   * @param GlobalContainer $gc
38
   */
39
  public function __construct(GlobalContainer $gc) {
40
    $this->gc    = $gc;
41
    $this->skins = array();
42
43
    // Берем текущий скин
44
    $this->activeSkin = $this->getSkin(SN::$gc->theUser->getSkinPath());
45
  }
46
47
  /**
48
   * Returns skin with skin name. Loads it - if it is required
49
   *
50
   * @param string $skinName
51
   *
52
   * @return SkinInterface
53
   */
54
  public function getSkin($skinName) {
55
    $skinName = $this->sanitizeSkinName($skinName);
56
57
    if (empty($this->skins[$skinName])) {
58
      // Прогружаем текущий скин
59
      $this->skins[$skinName] = $this->loadSkin($skinName);
60
    }
61
62
    return $this->skins[$skinName];
63
  }
64
65
  /**
66
   * @param string        $image_tag
67
   * @param template|null $template
68
   *
69
   * @return string
70
   */
71
  public function getImageCurrent($image_tag, $template = null) {
72
    return $this->getImageFrom($this->activeSkin->getName(), $image_tag, $template);
73
  }
74
75
  /**
76
   * @param string        $image_tag
77
   * @param template|null $template
78
   *
79
   * @return bool
80
   */
81
  public function isImageFileExists($image_tag, $template = null) {
82
    $largeUrl = $this->getImageCurrent($image_tag, $template);
83
    if (strpos($largeUrl, SKIN_IMAGE_MISSED_FILE_PATH) !== false) {
84
      // Image not found in directory
85
      $result = false;
86
    } else {
87
      // Image found in directory. But what for actual file?
88
      $imageRelativePath = substr($largeUrl, strlen(SN_ROOT_VIRTUAL));
89
      $result            = file_exists(SN_ROOT_PHYSICAL . $imageRelativePath);
90
    }
91
92
    return $result;
93
  }
94
95
  public function getImageFrom($skinName, $image_tag, $template) {
96
    return $this->getSkin($skinName)->imageFromStringTag($image_tag, $template);
97
  }
98
99
  /**
100
   * Switches current skin
101
   *
102
   * @param $skinName
103
   */
104
  public function switchActive($skinName) {
105
    $this->activeSkin = $this->getSkin($skinName);
106
  }
107
108
  /**
109
   * Loads skin
110
   *
111
   * @param string $skinName
112
   *
113
   * @return SkinInterface
114
   */
115
  protected function loadSkin($skinName) {
116
    $skinClass = $this->gc->skinEntityClass;
117
118
    $skin = new $skinClass($skinName, $this);
119
120
//    $skin->load();
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
121
122
    return $skin;
123
  }
124
125
126
  /**
127
   * @param string $skinName
128
   *
129
   * @return string
130
   */
131
  protected function sanitizeSkinName($skinName) {
132
    strpos($skinName, 'skins/') !== false ? $skinName = substr($skinName, 6) : false;
133
    strpos($skinName, '/') !== false ? $skinName = str_replace('/', '', $skinName) : false;
134
135
    return is_string($skinName) ? $skinName : '';
0 ignored issues
show
introduced by
The condition is_string($skinName) is always true.
Loading history...
136
  }
137
138
}
139