Completed
Pull Request — master (#16)
by Sergii
04:26
created

Drupal8Placeholder::switchMailSystem()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 3
eloc 11
nc 3
nop 1
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\Core\Field\FieldDefinitionInterface;
9
use Drupal\Core\Entity\FieldableEntityInterface;
10
use Drupal\Core\Session\AccountInterface;
11
use Drupal\Core\Database\Database;
12
use Drupal\Core\Database\Query\Select;
13
use Drupal\Core\StringTranslation\TranslatableMarkup;
14
use Drupal\Component\Render\FormattableMarkup;
15
use Drupal\Component\Serialization\Json;
16
use Drupal\TqExtension\Utils\Database\FetchField;
17
18
final class Drupal8Placeholder extends DrupalKernelPlaceholder
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public static function beforeFeature(BeforeFeatureScope $scope)
24
    {
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public static function t($string, array $args = [], array $options = [])
31
    {
32
        return (string) new TranslatableMarkup($string, $args, $options);
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public static function formatString($string, array $args = [])
39
    {
40
        return (string) new FormattableMarkup($string, $args);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public static function arg()
47
    {
48
        return explode('/', \Drupal::service('path.current')->getPath());
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public static function tokenReplace($text, array $data = [], array $options = [])
55
    {
56
        return \Drupal::token()->replace($text, $data, $options);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public static function sitePath()
63
    {
64
        return \Drupal::service('site.path');
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public static function jsonEncode($data)
71
    {
72
        return Json::encode($data);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     *
78
     * @param AccountInterface $user
79
     */
80
    public static function setCurrentUser($user)
81
    {
82
        \Drupal::currentUser()->setAccount($user);
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public static function setCurrentPath($path)
89
    {
90
        \Drupal::service('path.current')->setPath($path);
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public static function getUidByName($username)
97
    {
98
        return (int) (new FetchField('users_field_data', 'uid'))
99
            ->condition('name', $username)
100
            ->execute();
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public static function deleteUser($user_id)
107
    {
108
        $user_storage = \Drupal::entityTypeManager()->getStorage('user');
109
        $user_storage->delete([$user_storage->load($user_id)]);
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     *
115
     * @return Select
116
     */
117
    public static function selectQuery($table, $alias = null, array $options = [])
118
    {
119
        return \Drupal::database()->select($table, $alias, $options);
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public static function getFieldDefinitions($entityType, $bundle)
126
    {
127
        $definitions = [];
128
129
        /** @var FieldDefinitionInterface $definition */
130
        foreach (\Drupal::service('entity_field.manager')->getFieldDefinitions($entityType, $bundle) as $name => $definition) {
131
            $definitions[$name] = [
132
                'label' => (string) $definition->getLabel(),
133
                'required' => $definition->isRequired(),
134
            ];
135
        }
136
137
        return $definitions;
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public static function getDatabaseConnectionInfo($connection)
144
    {
145
        return Database::getConnectionInfo($connection);
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public static function entityLoad($entityType, $id)
152
    {
153
        return \Drupal::entityTypeManager()->getStorage($entityType)->load($id);
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     *
159
     * @param FieldableEntityInterface $entity
160
     */
161
    public static function entityHasField($entity, $fieldName)
162
    {
163
        return $entity->hasField($fieldName);
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     *
169
     * @param FieldableEntityInterface $entity
170
     */
171
    public static function entityFieldValue($entity, $fieldName)
172
    {
173
        return $entity->get($fieldName)->value;
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179
    public static function switchMailSystem($useTesting)
180
    {
181
        static $original = 'php_mail';
182
183
        $systemMail = \Drupal::configFactory()
184
          ->getEditable('system.mail');
185
186
        if ($useTesting) {
187
            // Store original mail system to restore it after scenario.
188
            $original = $systemMail->get('interface.default') ?: $original;
189
            // Set the mail system for testing. It will store an emails in
190
            // "system.test_mail_collector" Drupal state instead of sending.
191
            $value = 'test_mail_collector';
192
        } else {
193
            // Bring back the original mail system.
194
            $value = $original;
195
            // Flush the email buffer to be able to reuse it from scratch.
196
            // @see \Drupal\Core\Mail\Plugin\Mail\TestMailCollector
197
            \Drupal::state()->set('system.test_mail_collector', []);
198
        }
199
200
        $systemMail->set('interface.default', $value)->save(true);
201
    }
202
203
    /**
204
     * {@inheritdoc}
205
     */
206
    public static function getEmailMessages()
207
    {
208
        return \Drupal::state()->get('system.test_mail_collector') ?: [];
209
    }
210
211
    /**
212
     * {@inheritdoc}
213
     */
214
    public static function getContentTypeName($contentType)
215
    {
216
        $nodeTypeStorage = \Drupal::entityTypeManager()
217
            ->getStorage('node_type');
218
219
        if ($nodeTypeStorage->load($contentType)) {
220
            return $contentType;
221
        }
222
223
        // The result will be like: ['article' => 'article'].
224
        $result = $nodeTypeStorage->getQuery()
225
            ->condition('name', $contentType)
226
            ->execute();
227
228
        return empty($result) ? '' : reset($result);
229
    }
230
231
    /**
232
     * {@inheritdoc}
233
     */
234
    public static function injectCustomJavascript($file, $delete = false)
235
    {
236
        // @todo
237
    }
238
}
239