CroppableBehavior::modifyTable()   B
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 8.7972
cc 4
eloc 13
nc 5
nop 0
1
<?php
2
3
namespace Fenrizbes\CropBundle\Behavior;
4
5
use Fenrizbes\UploadableBehavior\Behavior\UploadableBehavior;
6
7
class CroppableBehavior extends \Behavior
8
{
9
    /**
10
     * {@inheritdoc}
11
     */
12
    protected $tableModificationOrder = 40;
13
14
    /**
15
     * {@inheritdoc}
16
     */
17
    protected $parameters = array(
18
        'columns' => 'image'
19
    );
20
21
    /**
22
     * Array of columns
23
     *
24
     * @var array
25
     */
26
    protected $columns;
27
28
    protected function getColumns()
29
    {
30
        if (is_null($this->columns)) {
31
            $this->columns = array();
32
33
            foreach (explode(',', $this->getParameter('columns')) as $column) {
34
                $this->columns[trim($column)] = trim($column) .'_coordinates';
35
            }
36
        }
37
38
        return $this->columns;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function modifyTable()
45
    {
46
        $table = $this->getTable();
47
48
        foreach ($this->getColumns() as $image_column => $coordinates_column) {
49
            if (!$table->hasColumn($image_column)) {
50
                $table->addColumn(array(
51
                    'name' => $image_column,
52
                    'type' => 'VARCHAR',
53
                    'size' => 255
54
                ));
55
            }
56
57
            if (!$table->hasColumn($coordinates_column)) {
58
                $table->addColumn(array(
59
                    'name' => $coordinates_column,
60
                    'type' => 'OBJECT'
61
                ));
62
            }
63
        }
64
65
        $this->modifyUploadableBehavior();
66
    }
67
68
    /**
69
     * Adds or modifies the uploadable behavior
70
     */
71
    protected function modifyUploadableBehavior()
72
    {
73
        $table = $this->getTable();
74
75
        if ($table->hasBehavior('uploadable')) {
76
            $uploadable = $table->getBehavior('uploadable');
77
            $parameters = $uploadable->getParameters();
78
            $up_columns = explode(',', $parameters['columns']);
79
80
            foreach (array_keys($this->getColumns()) as $column) {
81
                foreach ($up_columns as $up_column) {
82
                    if (trim($up_column) == $column) {
83
                        continue 2;
84
                    }
85
                }
86
87
                $up_columns[] = $column;
88
            }
89
90
            $parameters['columns'] = implode(',', $up_columns);
91
            $uploadable->setParameters($parameters);
92
        } else {
93
            $uploadable = new UploadableBehavior();
94
            $uploadable->setName('uploadable');
95
            $uploadable->setParameters(array(
96
                'columns' => implode(',', array_keys($this->getColumns()))
97
            ));
98
99
            $table->addBehavior($uploadable);
100
        }
101
    }
102
103
    /**
104
     * Constructs methods
105
     *
106
     * @return string
107
     */
108
    public function objectMethods()
109
    {
110
        $script = '';
111
112
        foreach ($this->getColumns() as $image_column => $coordinates_column) {
113
            $script .= $this->renderTemplate('setCroppableData', array(
114
                'image_column'       => $this->getTable()->getColumn($image_column)->getPhpName(),
115
                'coordinates_column' => $this->getTable()->getColumn($coordinates_column)->getPhpName()
116
            ));
117
118
            $script .= $this->renderTemplate('getCroppableData', array(
119
                'image_column'       => $this->getTable()->getColumn($image_column)->getPhpName(),
120
                'coordinates_column' => $this->getTable()->getColumn($coordinates_column)->getPhpName()
121
            ));
122
        }
123
124
        return $script;
125
    }
126
}