Completed
Push — master ( f0b491...11ee78 )
by Thomas
09:37
created

User::findActivityObject()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 22
ccs 0
cts 4
cp 0
rs 8.9197
cc 4
eloc 14
nc 6
nop 1
crap 20
1
<?php
2
3
namespace keeko\core\model;
4
5
use keeko\core\model\Base\User as BaseUser;
6
use keeko\core\model\types\ActivityObjectInterface;
7
use Symfony\Component\OptionsResolver\OptionsResolver;
8
use keeko\core\model\types\APIModelInterface;
9
use keeko\core\model\serializer\UserSerializer;
10
11
/**
12
 * Skeleton subclass for representing a row from the 'kk_user' table.
13
 *
14
 *
15
 *
16
 * You should add additional methods to this class to meet the
17
 * application requirements.  This class will only be generated as
18
 * long as it does not already exist in the output directory.
19
 *
20
 */
21
class User extends BaseUser implements APIModelInterface {
22
23
	private static $serializer = null;
24
	
25
	public static function getSerializer() {
26
		if (self::$serializer === null) {
27
			self::$serializer = new UserSerializer();
28
		}
29
	
30
		return self::$serializer;
31
	}
32
	
33
	public function newActivity(array $activity) {
34
		$resolver = new OptionsResolver();
35
		$resolver->setRequired(['verb', 'object']);
36
		$resolver->setOptional(['target']);
0 ignored issues
show
Bug introduced by
The method setOptional() does not seem to exist on object<Symfony\Component...solver\OptionsResolver>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
37
		$resolver->setAllowedTypes([
0 ignored issues
show
Bug introduced by
The call to setAllowedTypes() misses a required argument $allowedTypes.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
array('target' => array(...odel\\ActivityObject')) is of type array<string,array<integ...",\"1\":\"string\"}>"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
38
			'target' => ['keeko\core\model\types\ActivityObjectInterface', 'keeko\core\model\ActivityObject'],
39
			'object' => ['keeko\core\model\types\ActivityObjectInterface', 'keeko\core\model\ActivityObject']
40
		]);
41
		$options = $resolver->resolve($activity);
42
43
		$obj = new Activity();
44
		$obj->setActor($this);
45
		$obj->setVerb($options['verb']);
46
		$obj->setObject($this->getActivityObject($options['object']));
47
48
		if (isset($options['target'])) {
49
			$obj->setTarget($this->getActivityObject($options['target']));
50
		}
51
52
		$obj->save();
53
	}
54
55
	private function getActivityObject($obj) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
56
		if ($obj instanceof ActivityObject) {
57
			return $obj;
58
		}
59
		
60
		if ($obj instanceof ActivityObjectInterface) {
61
			return $this->findActivityObject($obj->toActivityObject());
62
		}
63
	}
64
65
	private function findActivityObject(ActivityObject $ao) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
66
		$q = ActivityObjectQuery::create()
67
			->filterByClassName($ao->getClassName())
68
			->filterByType($ao->getType())
69
			->filterByReferenceId($ao->getId());
70
71
		if (method_exists($ao, 'getVersion')) {
72
			$version = $ao->getVersion();
73
			if (!empty($version)) {
74
				$q = $q->filterByVersion($version);
75
			}
76
		}
77
78
		$result = $q->findOne();
79
80
		if ($result) {
81
			$result->setDisplayName($ao->getDisplayName());
82
			return $result;
83
		}
84
		
85
		return $ao;
86
	}
87
88
}
89