Completed
Pull Request — master (#139)
by
unknown
10:24
created

MarketModel::getInstanceClassName()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
rs 8.2222
cc 7
eloc 10
nc 5
nop 1
1
<?php
2
3
namespace Yandex\Market\Content\Models\Base;
4
5
use Yandex\Common\Model;
6
use Yandex\Market\Content\Models\ModelSingle;
7
use Yandex\Market\Content\Models\ModelChild;
8
use Yandex\Market\Content\Models\ModelParent;
9
use Yandex\Market\Content\Models\ModelVisual;
10
use Yandex\Market\Content\Models\ModelInfo;
11
12
/**
13
 * Class MarketModel
14
 *
15
 *
16
 * @package  Yandex\Market\Content\Models\Base
17
 *
18
 * @author   Oleg Scherbakov <[email protected]>
19
 * @created  03.01.16 04:23
20
 *
21
 *
22
 *
23
 *
24
    ----------------------------------Instance--------------------------------------
25
    |  | ModelSingle      | ModelParent     | ModelChildren     | ModelVisual
26
    --------------------------------------------------------------------------------
27
    |  | offersCount	  | offersCount	    | offersCount       | offersCount
28
    --------------------------------------------------------------------------------
29
    |  | rating           | rating	        | rating            | -
30
    --------------------------------------------------------------------------------
31
    |  | reviewsCount     | reviewsCount    | reviewsCount      | -
32
    --------------------------------------------------------------------------------
33
    |  | articlesCount	  | articlesCount	| articlesCount     | -
34
    --------------------------------------------------------------------------------
35
    |  | isNew            | isNew	        | isNew             | -
36
    --------------------------------------------------------------------------------
37
     P | vendorId         | vendorId	    | vendorId	        | vendorId
38
    --------------------------------------------------------------------------------
39
     R | gradeCount	      | gradeCount	    | gradeCount        | -
40
    --------------------------------------------------------------------------------
41
     O | categoryId       | categoryId	    | categoryId	    | categoryId
42
    --------------------------------------------------------------------------------
43
     P | id	              | id	            | id	            | id
44
    --------------------------------------------------------------------------------
45
     E | photos	          | photos	        | photos	        | photos
46
    --------------------------------------------------------------------------------
47
     R | link	          | link	        | link	            | link
48
    --------------------------------------------------------------------------------
49
     T | isGroup	      | isGroup	        | isGroup           | -
50
    --------------------------------------------------------------------------------
51
     I | vendor	          | vendor	        | vendor	        | vendorName
52
    --------------------------------------------------------------------------------
53
     E | name	          | name	        | name	            | name
54
    --------------------------------------------------------------------------------
55
     S | prices	          | prices	        | prices	        | prices
56
    --------------------------------------------------------------------------------
57
    |  | description	  | description	    | description	    | description
58
    --------------------------------------------------------------------------------
59
    |  | facts	          | facts	        | facts             | -
60
    --------------------------------------------------------------------------------
61
    |  | mainPhoto	      | mainPhoto	    | mainPhoto         | -
62
    --------------------------------------------------------------------------------
63
    |  | -                | children	    | parentModel       | -
64
    --------------------------------------------------------------------------------
65
    |  | -		          | -               | -                 | previewPhotos
66
    --------------------------------------------------------------------------------
67
    |  | -		          | -               | -                 | filters
68
    --------------------------------------------------------------------------------
69
    |  | -		          | -               | -                 | offers
70
 *
71
 */
72
class MarketModel extends Model
73
{
74
    /**
75
     * Return instance of model according to array structure.
76
     *
77
     * @param array $data
78
     * @return ModelChild|ModelParent|ModelSingle|ModelVisual|ModelInfo
79
     */
80
    public static function getInstance($data = array())
81
    {
82
        $className = self::getInstanceClassName($data);
83
84
        return new $className($data);
85
    }
86
87
    public static function getInstanceClassName($data = array())
88
    {
89
        if (isset($data['children'])|| isset($data['modificationsCount']))
90
            return 'Yandex\Market\Content\Models\ModelParent';
91
92
        if (isset($data['parentModel']))
93
            return 'Yandex\Market\Content\Models\ModelChild';
94
95
        if (isset($data['offers']))
96
            return 'Yandex\Market\Content\Models\ModelVisual';
97
98
        if (isset($data['offerCount']) || isset($data['type']))
99
            return 'Yandex\Market\Content\Models\ModelInfo';
100
101
        return 'Yandex\Market\Content\Models\ModelSingle';
102
    }
103
}
104