RawNodeContext   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 0
cbo 3
dl 0
loc 43
ccs 0
cts 14
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A entityType() 0 4 1
A getIdByArguments() 0 20 4
1
<?php
2
/**
3
 * @author Sergii Bondarenko, <[email protected]>
4
 */
5
namespace Drupal\TqExtension\Context\Node;
6
7
// Contexts.
8
use Drupal\TqExtension\Context\RawTqContext;
9
// Utils.
10
use Drupal\TqExtension\Utils\Database\FetchField;
11
use Drupal\TqExtension\Utils\BaseEntity;
12
13
class RawNodeContext extends RawTqContext
14
{
15
    use BaseEntity;
16
17
    /**
18
     * {@inheritdoc}
19
     */
20
    protected function entityType()
21
    {
22
        return 'node';
23
    }
24
25
    /**
26
     * @param string $title
27
     *   Inaccurate title of a node.
28
     * @param string $contentType
29
     *   Content type. Could be a title of content type.
30
     *
31
     * @return int
32
     *
33
     * @throws \InvalidArgumentException
34
     */
35
    public function getIdByArguments($title, $contentType = '')
36
    {
37
        $nid = new FetchField('node', 'nid');
38
        $nid->condition('title', "$title%", 'like');
39
40
        // Try to recognize node type by its title if content type specified and does not exist.
41
        if ('' !== $contentType && !isset(node_type_get_types()[$contentType])) {
42
            $contentType = (new FetchField('node_type', 'type'))
43
                ->condition('name', $contentType)
44
                ->execute();
45
46
            if ('' === $contentType) {
47
                throw new \InvalidArgumentException('Content type with such name does not exist!');
48
            }
49
50
            $nid->condition('type', $contentType);
51
        }
52
53
        return $nid->execute();
54
    }
55
}
56