Pages::hasPageWithId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 1
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Mediawiki\DataModel;
4
5
use InvalidArgumentException;
6
use RuntimeException;
7
8
/**
9
 * Represents a collection or Page classes
10
 *
11
 * @author Addshore
12
 */
13
class Pages {
14
15
	/**
16
	 * @var Page[]
17
	 */
18
	private $pages;
19
20
	/**
21
	 * @param Page[] $pages
22 4
	 */
23 4
	public function __construct( $pages = [] ) {
24 4
		$this->pages = [];
25 4
		$this->addPages( $pages );
26
	}
27
28
	/**
29
	 * @param Page[]|Pages $pages
30
	 *
31
	 * @throws InvalidArgumentException
32 4
	 */
33 4 View Code Duplication
	public function addPages( $pages ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
		if ( !is_array( $pages ) && !$pages instanceof Pages ) {
35
			throw new InvalidArgumentException( '$pages needs to either be an array or a Pages object' );
36 4
		}
37 1
		if ( $pages instanceof Pages ) {
38 1
			$pages = $pages->toArray();
39 4
		}
40 4
		foreach ( $pages as $page ) {
41 4
			$this->addPage( $page );
42 4
		}
43
	}
44
45
	/**
46
	 * @param Page $page
47 4
	 */
48 4
	public function addPage( Page $page ) {
49 4
		$this->pages[$page->getId()] = $page;
0 ignored issues
show
Deprecated Code introduced by
The method Mediawiki\DataModel\Page::getId() has been deprecated with message: since 0.5

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
50
	}
51
52
	/**
53
	 * @param int $id
54
	 *
55
	 * @return bool
56
	 */
57
	public function hasPageWithId( $id ) {
58
		return array_key_exists( $id, $this->pages );
59
	}
60
61
	/**
62
	 * @param Page $page
63
	 *
64
	 * @return bool
65
	 */
66
	public function hasPage( Page $page ) {
67
		return array_key_exists( $page->getId(), $this->pages );
0 ignored issues
show
Deprecated Code introduced by
The method Mediawiki\DataModel\Page::getId() has been deprecated with message: since 0.5

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
68
	}
69
70
	/**
71
	 * @return Page|null Page or null if there is no page
72
	 */
73
	public function getLatest() {
74
		if ( empty( $this->pages ) ) {
75
			return null;
76
		}
77
		return $this->pages[ max( array_keys( $this->pages ) ) ];
78
	}
79
80
	/**
81
	 * @param int $pageid
82
	 *
83
	 * @throws RuntimeException
84
	 * @return Page
85
	 */
86
	public function get( $pageid ) {
87
		if ( $this->hasPageWithId( $pageid ) ) {
88
			return $this->pages[$pageid];
89
		}
90
		throw new RuntimeException( 'No such page loaded in Pages object' );
91
	}
92
93
	/**
94
	 * @return Page[]
95
	 */
96 4
	public function toArray() {
97 4
		return $this->pages;
98
	}
99
}
100