Failed Conditions
Push — develop ( d7a728...605806 )
by Elvis Henrique
04:12
created

Post::find_by_author_id()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 15
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * Post.
4
 *
5
 * @package App
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Repositories;
11
12
use App\Entities\Collection;
13
use App\Entities\Post as Entity;
14
15
/**
16
 * Post class.
17
 */
18
class Post extends AbstractPost {
19
20
	/**
21
	 * Find by id.
22
	 *
23
	 * @param integer $id Id.
24
	 * @return Entity|null
25
	 */
26 2
	public function find_by_id( int $id ) : ?Entity {
27 2
		$post = $this->get_post( $id );
28
29 2
		if ( ! $post ) {
0 ignored issues
show
introduced by
$post is of type WP_Post, thus it always evaluated to true.
Loading history...
30 1
			return null;
31
		}
32
33 1
		return ( new Entity() )->set_post( $post );
34
	}
35
36
	/**
37
	 * Find by author id.
38
	 *
39
	 * @param integer $author_id Author id.
40
	 * @param integer $quantity Quantity.
41
	 * @return Collection
42
	 */
43 1
	public function find_by_author_id( int $author_id, int $quantity ) : Collection {
44 1
		$posts = $this->get_posts(
45
			[
46 1
				'numberposts' => $quantity,
47 1
				'author'      => $author_id,
48
			]
49
		);
50
51 1
		$collection = new Collection();
52
53 1
		foreach ( $posts as $post ) {
54 1
			$collection->add_entity( ( new Entity() )->set_post( $post ) );
55
		}
56
57 1
		return $collection;
58
	}
59
}
60