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

RawNodeContext::getNodeIdByTitle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 14
rs 9.4285
cc 2
eloc 10
nc 2
nop 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