Completed
Pull Request — master (#6)
by
unknown
02:17
created

RawNodeContext   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A getNodeIdByTitle() 0 14 2
1
<?php
2
/**
3
 * @author Cristina Eftimita, <[email protected]>
4
 */
5
namespace Drupal\TqExtension\Context\Node;
6
7
// Contexts.
8
use Drupal\TqExtension\Context\RawTqContext;
9
10
class RawNodeContext extends RawTqContext
11
{
12
    /**
13
     * Retrieves node nid by node title and type.
14
     * @param $title
15
     *   Node title to lookup, accepts exact match only.
16
     * @param $type
17
     *   Node type machine name.
18
     * @return int|null
19
     *   Returns node nid if found.
20
     * @throws \Exception
21
     */
22
    public function getNodeIdByTitle($title, $type)
23
    {
24
        $nid = db_select('node', 'n')
25
            ->fields('n', array('nid'))
26
            ->condition('n.title', $title)
27
            ->condition('n.type', $type)
28
            ->range(0, 1)
29
            ->execute()->fetchField();
30
31
        if (empty($nid)) {
32
            throw new \Exception(sprintf("Node %s of %s not found.", $title, $type));
33
        }
34
        return $nid;
35
    }
36
}
37