Completed
Push — master ( 7ef224...a54477 )
by Vitaly
02:34
created

CMS::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4286
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace samsoncms\api;
3
4
use samson\activerecord\dbRelation;
5
use samson\activerecord\field;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, samsoncms\api\field.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use samson\activerecord\materialfield;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, samsoncms\api\materialfield.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use samson\activerecord\structure;
8
use samson\activerecord\structurefield;
9
use samson\activerecord\structurematerial;
10
use samson\activerecord\TableRelation;
11
use samson\activerecord\material;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, samsoncms\api\material.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
use samson\core\CompressableService;
13
use samson\activerecord\dbRecord;
14
use samson\activerecord\dbMySQLConnector;
15
16
/**
17
 * SamsonCMS API
18
 * @package samsoncms\api
19
 */
20
class CMS extends CompressableService
21
{
22
    /** Identifier */
23
    protected $id = 'cmsapi';
24
25
    /**
26
     * Read SQL file with variables placeholders pasting
27
     * @param string $filePath SQL file for reading
28
     * @param string $prefix Prefix for addition
29
     * @return string SQL command text
30
     */
31
    public function readSQL($filePath, $prefix = '')
32
    {
33
        $sql = '';
34
35
        // Build path to SQL folder
36
        if (file_exists($filePath)) {
37
            // Replace prefix
38
            $sql = str_replace('@prefix', $prefix, file_get_contents($filePath));
39
        }
40
41
        return $sql;
42
    }
43
44
    /**
45
     * @see ModuleConnector::prepare()
46
     */
47
    public function prepare()
48
    {
49
        // Perform SQL table creation
50
        $path = __DIR__.'/../sql/';
51
        foreach (array_slice(scandir($path), 2) as $file) {
52
            db()->query($this->readSQL($path.$file, $this->tablePrefix));
53
        }
54
55
        // Initiate migration mechanism
56
        db()->migration(get_class($this), array($this, 'migrator'));
57
58
        // Define permanent table relations
59
        new TableRelation('material', 'user', 'UserID', 0, 'user_id');
60
        new TableRelation('material', 'gallery', 'MaterialID', TableRelation::T_ONE_TO_MANY);
61
        new TableRelation('material', 'materialfield', 'MaterialID', TableRelation::T_ONE_TO_MANY);
62
        new TableRelation('material', 'field', 'materialfield.FieldID', TableRelation::T_ONE_TO_MANY);
63
        new TableRelation('material', 'structurematerial', 'MaterialID', TableRelation::T_ONE_TO_MANY);
64
        new TableRelation('material', 'structure', 'structurematerial.StructureID', TableRelation::T_ONE_TO_MANY);
65
        new TableRelation('materialfield', 'field', 'FieldID');
66
        new TableRelation('materialfield', 'material', 'MaterialID');
67
        new TableRelation('structurematerial', 'structure', 'StructureID');
68
        new TableRelation('structurematerial', 'materialfield', 'MaterialID', TableRelation::T_ONE_TO_MANY);
69
        new TableRelation('structurematerial', 'material', 'MaterialID', TableRelation::T_ONE_TO_MANY);
70
        new TableRelation('structure', 'material', 'structurematerial.MaterialID', TableRelation::T_ONE_TO_MANY, null, 'manymaterials');
71
        new TableRelation('structure', 'gallery', 'structurematerial.MaterialID', TableRelation::T_ONE_TO_MANY, null, 'manymaterials');
72
        /*new TableRelation( 'structure', 'material', 'MaterialID' );*/
73
        new TableRelation('structure', 'user', 'UserID', 0, 'user_id');
74
        new TableRelation('structure', 'materialfield', 'material.MaterialID', TableRelation::T_ONE_TO_MANY, 'MaterialID', '_mf');
75
        new TableRelation('structure', 'structurematerial', 'StructureID', TableRelation::T_ONE_TO_MANY);
76
        new TableRelation('related_materials', 'material', 'first_material', TableRelation::T_ONE_TO_MANY, 'MaterialID');
77
        new TableRelation('related_materials', 'materialfield', 'first_material', TableRelation::T_ONE_TO_MANY, 'MaterialID');
78
        new TableRelation('field', 'structurefield', 'FieldID');
79
        new TableRelation('field', 'structure', 'structurefield.StructureID');
80
        new TableRelation('structurefield', 'field', 'FieldID');
81
        new TableRelation('structurefield', 'materialfield', 'FieldID');
82
        new TableRelation('structurefield', 'material', 'materialfield.MaterialID');
83
        new TableRelation('structure', 'structure_relation', 'StructureID', TableRelation::T_ONE_TO_MANY, 'parent_id', 'children_relations');
84
        new TableRelation('structure', 'structure', 'children_relations.child_id', TableRelation::T_ONE_TO_MANY, 'StructureID', 'children');
85
        new TableRelation('structure', 'structure_relation', 'StructureID', TableRelation::T_ONE_TO_MANY, 'child_id', 'parents_relations');
86
        new TableRelation('structure', 'structure', 'parents_relations.parent_id', TableRelation::T_ONE_TO_MANY, 'StructureID', 'parents');
87
        new TableRelation('structurematerial', 'structure_relation', 'StructureID', TableRelation::T_ONE_TO_MANY, 'parent_id');
88
        new TableRelation('groupright', 'right', 'RightID', TableRelation::T_ONE_TO_MANY);
89
        //elapsed('CMS:prepare');
90
91
        // Все прошло успешно
92
        return true && parent::prepare();
93
    }
94
95
    /**
96
     * Handler for CMSAPI database version manipulating
97
     * @param string $to_version Version to switch to
98
     * @return string Current database version
99
     */
100
    public function migrator($to_version = null)
101
    {
102
        // If something passed - change database version to it
103
        if (func_num_args()) {
104
            // Save current version to special db table
105
            db()->query("ALTER TABLE  `" . dbMySQLConnector::$prefix . "cms_version` CHANGE  `version`  `version` VARCHAR( 15 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT  '" . $to_version . "';");
106
            die('Database successfully migrated to [' . $to_version . ']');
107
        } else { // Return current database version
108
            $version_row = db()->fetchOne('SHOW COLUMNS FROM `' . dbMySQLConnector::$prefix . 'cms_version`');
109
            if (isset($version_row[0]['Default'])) {
110
                return $version_row[0]['Default'];
111
            } else {
112
                return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by samsoncms\api\CMS::migrator of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
113
            }
114
        }
115
    }
116
}
117