Test Setup Failed
Push — master ( 6592af...c06444 )
by Chauncey
08:19
created

MultiObjectProperty::getAllowedTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Charcoal\Property;
4
5
use InvalidArgumentException;
6
7
/**
8
 * Multi-Object Property holds references to external objects from different models.
9
 */
10
class MultiObjectProperty extends AbstractProperty
11
{
12
    /**
13
     * @var array $allowedTypes
14
     */
15
    private $allowedTypes;
16
17
    /**
18
     * @var boolean $groupedByType
19
     */
20
    private $groupedByType = true;
21
22
    /**
23
     * @var string $joinTable
24
     */
25
    private $joinTable = 'charcoal_multi_objects';
26
27
    /**
28
     * @param array $types The allowed types map.
29
     * @return MultiObjectProperty Chainable
30
     */
31
    public function setAllowedTypes(array $types)
32
    {
33
        foreach ($types as $type => $typeOptions) {
34
            $this->addAllowedType($type, $typeOptions);
35
        }
36
        return $this;
37
    }
38
39
    /**
40
     * @param string $type        The (allowed) object type.
41
     * @param array  $typeOptions Extra options for the type.
42
     * @return MultiObjectProperty Chainable
43
     */
44
    public function addAllowedType($type, array $typeOptions = [])
45
    {
46
        $this->allowedTypes[$type] = $typeOptions;
47
        return $this;
48
    }
49
50
    /**
51
     * @return array
52
     */
53
    public function getAllowedTypes()
54
    {
55
        return $this->allowedTypes;
56
    }
57
58
    /**
59
     * @param string $table The join table.
60
     * @throws InvalidArgumentException If the table is not a string or contains invalid table characters.
61
     * @return MultiObjectProperty Chainable
62
     */
63
    public function setJoinTable($table)
64
    {
65
        if (!is_string($table)) {
66
            throw new InvalidArgumentException(
67
                'Join table must be a string'
68
            );
69
        }
70
        // For security reason, only alphanumeric characters (+ underscores) are valid table names.
71
        // Although SQL can support more, there's really no reason to.
72
        if (!preg_match('/[A-Za-z0-9_]/', $table)) {
73
            throw new InvalidArgumentException(
74
                sprintf('Table name "%s" is invalid: must be alphanumeric / underscore.', $table)
75
            );
76
        }
77
        $this->joinTable = $table;
78
        return $this;
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function getJoinTable()
85
    {
86
        return $this->joinTable;
87
    }
88
89
    /**
90
     * Create the join table on the database source, if it does not exist.
91
     *
92
     * @return void
93
     */
94
    public function createJoinTable()
95
    {
96
        if ($this->joinTableExists() === true) {
97
            return;
98
        }
99
100
        $q = 'CREATE TABLE \''.$this->getJoinTable().'\' (
101
            target_type VARCHAR(255),
102
            target_id VARCHAR(255),
103
            target_property VARCHAR(255),
104
            attachment_type VARCHAR(255),
105
            attachment_id VARCHAR(255),
106
            created DATETIME
107
        )';
108
        $this->logger->debug($q);
109
        $this->source()->db()->query($q);
0 ignored issues
show
Bug introduced by
The method source() does not seem to exist on object<Charcoal\Property\MultiObjectProperty>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
110
    }
111
112
    /**
113
     * @return boolean
114
     */
115
    public function joinTableExists()
116
    {
117
        $q = 'SHOW TABLES LIKE \''.$this->getJoinTable().'\'';
118
        $this->logger->debug($q);
119
        $res = $this->source()->db()->query($q);
0 ignored issues
show
Bug introduced by
The method source() does not seem to exist on object<Charcoal\Property\MultiObjectProperty>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
120
        $tableExists = $res->fetchColumn(0);
121
122
        return !!$tableExists;
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    public function type()
129
    {
130
        return 'multi-object';
131
    }
132
133
    /**
134
     * @return string|null
135
     */
136
    public function sqlType()
137
    {
138
        return null;
139
    }
140
141
    /**
142
     * @return integer
143
     */
144
    public function sqlPdoType()
145
    {
146
        return 0;
147
    }
148
}
149