Test Failed
Branch trunk (412648)
by SuperNova.WS
03:40
created

Repository::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Created by Gorlum 10.02.2017 0:07
5
 */
6
7
namespace Core;
8
9
use \Common\ContainerPlus;
10
use \Common\GlobalContainer;
11
use TextEntity;
12
use TextModel;
13
14
/**
15
 * Class Core\Repository
16
 *
17
 * Holds current entity objects
18
 */
19
class Repository {
20
21
  /**
22
   * "Not an Object" marker
23
   */
24
  const NaO = '\\NaO';
25
26
27
  /**
28
   * @var ContainerPlus $_repository
29
   */
30
  protected $_repository;
31
32
 /**
33
   * @var ContainerPlus $_oldRepo
34
   */
35
  protected $_oldRepo;
36
37
  /**
38
   * @var GlobalContainer $gc
39
   */
40
  protected $gc;
41
42
  /**
43
   * Core\Repository constructor.
44
   *
45
   * @param GlobalContainer $gc
46
   */
47
  public function __construct(GlobalContainer $gc) {
48
    $this->gc = $gc;
49
  }
50
51
  /**
52
   * @param TextModel  $model
53
   * @param int|string $id
54
   *
55
   * @return TextEntity
56
   * @deprecated
57
   */
58
  public function getById($model, $id) {
59
60
    // TODO - is_object()
61
62
    // Index is fully qualified class name plus ID like Namespace\ClassName\$id
63
    $entityIndex = get_class($model) . '\\' . $id;
64
    if (!isset($this->_oldRepo[$entityIndex])) {
65
      $entity = $model->loadById($id);
66
      if ($entity && !$entity->isEmpty()) {
67
        $this->_oldRepo[$entityIndex] = $entity;
68
      }
69
    } else {
70
      $entity = $this->_oldRepo[$entityIndex];
71
    }
72
73
    return $entity;
74
  }
75
76
  /**
77
   * Returns collection name for supplied object
78
   *
79
   * @param $object
80
   *
81
   * @return string
82
   */
83
  protected function getCollectionName($object) {
84
    return is_object($object) ? get_class($object) : self::NaO;
85
  }
86
87
  public function get($entityClass, $id) {
88
    $entityIndex = get_class($entityClass) . '\\' . $id;
89
    if(!isset($this->_repository[$entityIndex])) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
90
91
    }
92
93
    return $this->_repository[$entityIndex];
94
  }
95
96
  protected function getPool($entityClass) {
0 ignored issues
show
Unused Code introduced by
The parameter $entityClass is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
97
98
  }
99
100
  public function registerFactory($factory) {
0 ignored issues
show
Unused Code introduced by
The parameter $factory is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
101
102
  }
103
104
}
105