Completed
Pull Request — master (#16)
by Sergii
07:37
created

DrupalKernelPlaceholder   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 322
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 0
dl 0
loc 322
rs 10
c 0
b 0
f 0

24 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeFeature() 0 4 1
A t() 0 4 1
A formatString() 0 4 1
A arg() 0 4 1
A tokenReplace() 0 4 1
A setCurrentUser() 0 4 1
A setCurrentPath() 0 4 1
A getUidByName() 0 4 1
A deleteUser() 0 4 1
A selectQuery() 0 4 1
A getFieldDefinitions() 0 4 1
A getDatabaseConnectionInfo() 0 4 1
A jsonEncode() 0 4 1
A getCurrentUser() 0 4 1
A entityCreate() 0 4 1
A entityLoad() 0 4 1
A entityHasField() 0 4 1
A entityFieldValue() 0 4 1
A switchMailSystem() 0 4 1
A getEmailMessages() 0 4 1
A getContentTypeName() 0 4 1
A getWatchdogStackTrace() 0 9 1
B injectCustomJavascript() 0 44 3
A requireContext() 0 10 2
1
<?php
2
/**
3
 * @author Sergii Bondarenko, <[email protected]>
4
 */
5
namespace Drupal\TqExtension\Cores;
6
7
use Behat\Behat\Hook\Scope\BeforeFeatureScope;
8
use Drupal\TqExtension\Context\TqContext;
9
10
class DrupalKernelPlaceholder
11
{
12
    /**
13
     * Version-related implementation of @BeforeFeature hook for TqContext.
14
     *
15
     * @param BeforeFeatureScope $scope
16
     *
17
     * @see TqContext::beforeFeature()
18
     */
19
    public static function beforeFeature(BeforeFeatureScope $scope)
20
    {
21
        self::requireContext(__FUNCTION__, func_get_args());
22
    }
23
24
    /**
25
     * @param string $string
26
     * @param array $arguments
27
     * @param array $options
28
     *
29
     * @return string
30
     */
31
    public static function t($string, array $arguments = [], array $options = [])
32
    {
33
        return self::requireContext(__FUNCTION__, func_get_args());
34
    }
35
36
    /**
37
     * @param string $string
38
     * @param array $arguments
39
     *
40
     * @return string
41
     */
42
    public static function formatString($string, array $arguments = [])
43
    {
44
        return self::requireContext(__FUNCTION__, func_get_args());
45
    }
46
47
    /**
48
     * @return string[]
49
     */
50
    public static function arg()
51
    {
52
        return self::requireContext(__FUNCTION__, func_get_args());
53
    }
54
55
    /**
56
     * @param string $text
57
     *   Input text.
58
     * @param array $data
59
     *   Data for replacements.
60
     * @param array $options
61
     *   Replacement configuration.
62
     *
63
     * @return string
64
     *   Processed text.
65
     */
66
    public static function tokenReplace($text, array $data = [], array $options = [])
67
    {
68
        return self::requireContext(__FUNCTION__, func_get_args());
69
    }
70
71
    /**
72
     * @param mixed $data
73
     *
74
     * @return string
75
     */
76
    public static function jsonEncode($data)
77
    {
78
        return self::requireContext(__FUNCTION__, func_get_args());
79
    }
80
81
    /**
82
     * @return object
83
     */
84
    public static function getCurrentUser()
85
    {
86
        return self::requireContext(__FUNCTION__, func_get_args());
87
    }
88
89
    public static function setCurrentUser($user)
90
    {
91
        self::requireContext(__FUNCTION__, func_get_args());
92
    }
93
94
    public static function setCurrentPath($path)
95
    {
96
        self::requireContext(__FUNCTION__, func_get_args());
97
    }
98
99
    /**
100
     * Locate user ID by its name.
101
     *
102
     * @param string $username
103
     *
104
     * @return int
105
     */
106
    public static function getUidByName($username)
107
    {
108
        return self::requireContext(__FUNCTION__, func_get_args());
109
    }
110
111
    /**
112
     * @param int $user_id
113
     */
114
    public static function deleteUser($user_id)
115
    {
116
        self::requireContext(__FUNCTION__, func_get_args());
117
    }
118
119
    /**
120
     * @param string $table
121
     * @param string $alias
122
     * @param array $options
123
     *
124
     * @return object
125
     */
126
    public static function selectQuery($table, $alias = null, array $options = [])
127
    {
128
        return self::requireContext(__FUNCTION__, func_get_args());
129
    }
130
131
    /**
132
     * @param string $entityType
133
     * @param string $bundle
134
     *
135
     * @return array[]
136
     *   An associative array where key - machine-name of a field and
137
     *   value - an array with two keys: "label" and "required".
138
     */
139
    public static function getFieldDefinitions($entityType, $bundle)
140
    {
141
        return self::requireContext(__FUNCTION__, func_get_args());
142
    }
143
144
    /**
145
     * Get information about database connections.
146
     *
147
     * Impossible to use $GLOBALS['databases'] in Drupal 8 since {@link https://www.drupal.org/node/2176621}.
148
     *
149
     * @param string $connection
150
     *   Connection name.
151
     *
152
     * @return array[]
153
     */
154
    public static function getDatabaseConnectionInfo($connection)
155
    {
156
        return (array) self::requireContext(__FUNCTION__, func_get_args());
157
    }
158
159
    /**
160
     * @param string $entityType
161
     *   The type of entity.
162
     * @param array $values
163
     *   Values for entity creation.
164
     *
165
     * @return string[]
166
     *   List with three items in order: entity ID, type and bundle.
167
     */
168
    public static function entityCreate($entityType, array $values)
169
    {
170
        return self::requireContext(__FUNCTION__, func_get_args());
171
    }
172
173
    /**
174
     * @param string $entityType
175
     *   The type of entity.
176
     * @param int $id
177
     *
178
     * @return object|null
179
     */
180
    public static function entityLoad($entityType, $id)
181
    {
182
        return self::requireContext(__FUNCTION__, func_get_args());
183
    }
184
185
    /**
186
     * @param object $entity
187
     * @param string $fieldName
188
     *
189
     * @return bool
190
     */
191
    public static function entityHasField($entity, $fieldName)
192
    {
193
        return self::requireContext(__FUNCTION__, func_get_args());
194
    }
195
196
    /**
197
     * @param object $entity
198
     * @param string $fieldName
199
     *
200
     * @return mixed
201
     */
202
    public static function entityFieldValue($entity, $fieldName)
203
    {
204
        return self::requireContext(__FUNCTION__, func_get_args());
205
    }
206
207
    /**
208
     * Switching the mail system.
209
     *
210
     * @param bool $useTesting
211
     *   Whether testing or standard mail system should be used.
212
     */
213
    public static function switchMailSystem($useTesting)
214
    {
215
        self::requireContext(__FUNCTION__, func_get_args());
216
    }
217
218
    /**
219
     * Get a list of emails, collected by testing mail system.
220
     *
221
     * @return array
222
     */
223
    public static function getEmailMessages()
224
    {
225
        return self::requireContext(__FUNCTION__, func_get_args());
226
    }
227
228
    /**
229
     * Check existence of the content type by its machine name or title.
230
     *
231
     * @param string $contentType
232
     *   Machine name or title of the content type.
233
     *
234
     * @return string
235
     *   Machine name.
236
     */
237
    public static function getContentTypeName($contentType)
238
    {
239
        return self::requireContext(__FUNCTION__, func_get_args());
240
    }
241
242
    /**
243
     * @param int $count
244
     *   Number of entries to fetch.
245
     * @param string[] $types
246
     *   Any set of Watchdog error types.
247
     *
248
     * @return array[]
249
     */
250
    public static function getWatchdogStackTrace($count = 10, array $types = ['php'])
251
    {
252
        return array_map('unserialize', static::selectQuery('watchdog', 'w')
253
            ->fields('w', ['variables'])
254
            ->condition('type', $types, 'IN')
255
            ->range(0, $count)
256
            ->execute()
257
            ->fetchCol());
258
    }
259
260
    /**
261
     * @param string $file
262
     *   Existing file from "src/JavaScript" without ".js" extension.
263
     * @param bool $delete
264
     *   Whether injection should be deleted.
265
     */
266
    final public static function injectCustomJavascript($file, $delete = false)
267
    {
268
        // Append extension.
269
        $file .= '.js';
270
        // Do manipulations with "system" module.
271
        $moduleName = 'system';
272
        // Get the relative path to module.
273
        $modulePath = drupal_get_path('module', $moduleName);
274
        // Put custom JS directly in the module folder.
275
        $destination = "$modulePath/$file";
276
277
        if (DRUPAL_CORE > 7) {
278
            // Find an unique line.
279
            $search = 'js/system.js: {}';
280
            // Insert after unique line.
281
            $injection = "    $file: {}";
282
            // Do injection in the "system.*?" file.
283
            $extension = 'libraries.yml';
284
        } else {
285
            $search = 'system_add_module_assets();';
286
            $injection = "drupal_add_js('$destination', ['every_page' => TRUE]);";
287
            $extension = 'module';
288
        }
289
290
        // Do an insertion on a new line.
291
        $injection = "\n$injection";
292
        // Form the filename for manipulations.
293
        $target = "$modulePath/$moduleName.$extension";
294
295
        if ($delete) {
296
            // Remove the file.
297
            unlink($destination);
298
            // Find an injection and replace it by emptiness.
299
            $search = $injection;
300
            $replace = '';
301
        } else {
302
            // Copy the file.
303
            copy(str_replace('Cores', 'JavaScript', __DIR__) . '/' . $file, $destination);
304
            // Find an unique line and append injection.
305
            $replace = $search . $injection;
306
        }
307
308
        file_put_contents($target, str_replace($search, $replace, file_get_contents($target)));
309
    }
310
311
    /**
312
     * Require method execution from context.
313
     *
314
     * @param string $method
315
     *   The name of method.
316
     * @param array $arguments
317
     *   Method's arguments.
318
     *
319
     * @return mixed
320
     */
321
    private static function requireContext($method, array $arguments)
322
    {
323
        $context = str_replace('Kernel', DRUPAL_CORE, __CLASS__);
324
325
        if (method_exists($context, $method)) {
326
            return call_user_func_array([$context, $method], $arguments);
327
        }
328
329
        throw new \BadMethodCallException(sprintf('Method "%s" is not implemented in "%s".', $method, $context));
330
    }
331
}
332