Completed
Pull Request — master (#114)
by Bart
02:30
created

ExportController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 56
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A actionIndex() 0 9 1
B exportToYaml() 0 27 4
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Controllers;
4
5
use Craft;
6
use craft\helpers\FileHelper;
7
use NerdsAndCompany\Schematic\Interfaces\DataTypeInterface;
8
use NerdsAndCompany\Schematic\Interfaces\MapperInterface;
9
use NerdsAndCompany\Schematic\Models\Data;
10
use NerdsAndCompany\Schematic\Schematic;
11
use Symfony\Component\Yaml\Yaml;
12
13
/**
14
 * Schematic Export Controller.
15
 *
16
 * Sync Craft Setups.
17
 *
18
 * @author    Nerds & Company
19
 * @copyright Copyright (c) 2015-2018, Nerds & Company
20
 * @license   MIT
21
 *
22
 * @see      http://www.nerds.company
23
 */
24
class ExportController extends Base
25
{
26
    /**
27
     * Exports the Craft datamodel.
28
     *
29
     * @param string $file    file to write the schema to
0 ignored issues
show
Bug introduced by
There is no parameter named $file. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
30
     * @param array  $exclude Data to not export
0 ignored issues
show
Bug introduced by
There is no parameter named $exclude. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
31
     *
32
     * @return int
33
     */
34
    public function actionIndex()
35
    {
36
        $dataTypes = $this->getDataTypes();
37
38
        $this->exportToYaml($this->file, $dataTypes);
39
        Schematic::info('Exported schema to '.$this->file);
40
41
        return 0;
42
    }
43
44
    /**
45
     * Export to Yaml file.
46
     *
47
     * @param string $file
48
     * @param bool   $autoCreate
0 ignored issues
show
Bug introduced by
There is no parameter named $autoCreate. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
49
     *
50
     * @return int
51
     */
52
    public function exportToYaml($file, $dataTypes)
53
    {
54
        $this->disableLogging();
55
        $result = [];
56
        foreach ($dataTypes as $dataTypeHandle) {
57
            $dataTypeClass = Schematic::DATA_TYPES[$dataTypeHandle];
58
            $dataType = new $dataTypeClass();
59
            if (!$dataType instanceof DataTypeInterface) {
60
                Schematic::error($dataTypeClass.' does not implement DataTypeInterface');
61
                continue;
62
            }
63
64
            $mapper = $dataType->getMapperHandle();
65
            if (!Craft::$app->controller->module->$mapper instanceof MapperInterface) {
66
                Schematic::error(get_class(Craft::$app->controller->module->$mapper).' does not implement MapperInterface');
67
                continue;
68
            }
69
70
            Schematic::info('Exporting '.$dataTypeHandle);
71
            $records = $dataType->getRecords();
72
            $result[$dataTypeHandle] = Craft::$app->controller->module->$mapper->export($records);
73
        }
74
75
        FileHelper::writeToFile($file, Data::toYaml($result));
76
77
        return 0;
78
    }
79
}
80