PostgreSqlDsn   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 85
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A toString() 0 13 3
A setUser() 0 9 2
A hasUser() 0 3 1
A getUser() 0 3 1
A setPassword() 0 9 2
A hasPassword() 0 3 1
A getPassword() 0 3 1
1
<?php
2
/**
3
 * This file is part of the Yep package.
4
 * Copyright (c) 2016 Martin Zeman (Zemistr) (http://www.zemistr.eu)
5
 */
6
7
namespace Yep\Dsn;
8
9
/**
10
 * Class PostgreSqlDsn
11
 *
12
 * @package Yep\Dsn
13
 * @author  Martin Zeman (Zemistr) <[email protected]>
14
 *
15
 * PDO_PGSQL
16
 * pgsql:host=localhost;port=5432;dbname=testdb;user=bruce;password=mypass
17
 */
18
class PostgreSqlDsn extends BaseDns {
19
  /** @var string */
20
  protected $user;
21
22
  /** @var string */
23
  protected $password;
24
25 4
  public function __construct(string $dbName, string $host = 'localhost', int $port = 5432, string $user = null, string $password = null) {
26 4
    $this->setDbName($dbName);
27 4
    $this->setHost($host);
28 4
    $this->setPort($port);
29 4
    $this->setUser($user);
30 4
    $this->setPassword($password);
31 4
  }
32
33 1
  public function toString() : string {
34 1
    $dsn = 'pgsql:host=' . $this->getHost() . ';dbname=' . $this->getDbName() . ';port=' . $this->getPort();
35
36 1
    if ($this->hasUser()) {
37 1
      $dsn .= ';user=' . $this->getUser();
38
    }
39
40 1
    if ($this->hasPassword()) {
41 1
      $dsn .= ';password=' . $this->getPassword();
42
    }
43
44 1
    return $dsn;
45
  }
46
47
  /**
48
   * @param string|null $user
49
   * @return PostgreSqlDsn
0 ignored issues
show
Documentation introduced by
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
50
   */
51 4
  public function setUser(string $user = null) : self {
52 4
    $this->user = $user;
53
54 4
    if ($user !== null) {
55 1
      $this->user = trim($user);
56
    }
57
58 4
    return $this;
59
  }
60
61
  /**
62
   * @return bool
63
   */
64 1
  public function hasUser() : bool {
65 1
    return $this->user !== null;
66
  }
67
68
  /**
69
   * @return string
70
   */
71 1
  public function getUser() : string {
72 1
    return (string)$this->user;
73
  }
74
75
  /**
76
   * @param string|null $password
77
   * @return PostgreSqlDsn
0 ignored issues
show
Documentation introduced by
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
78
   */
79 4
  public function setPassword(string $password = null) : self {
80 4
    $this->password = $password;
81
82 4
    if ($this->password !== null) {
83 1
      $this->password = trim($password);
84
    }
85
86 4
    return $this;
87
  }
88
89
  /**
90
   * @return bool
91
   */
92 1
  public function hasPassword() : bool {
93 1
    return $this->password !== null;
94
  }
95
96
  /**
97
   * @return string
98
   */
99 1
  public function getPassword() : string {
100 1
    return (string)$this->password;
101
  }
102
}
103