Completed
Push — master ( 5ec914...2c4a4d )
by Thomas
04:29
created

User::getSerializer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 7
rs 9.4285
ccs 0
cts 6
cp 0
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
namespace keeko\core\model;
3
4
use keeko\core\model\Base\User as BaseUser;
5
use keeko\core\serializer\UserSerializer;
6
use keeko\framework\model\ApiModelInterface;
7
use Symfony\Component\OptionsResolver\OptionsResolver;
8
use keeko\framework\model\ActivityObjectInterface;
9
10
/**
11
 * Skeleton subclass for representing a row from the 'kk_user' table.
12
 *
13
 * You should add additional methods to this class to meet the
14
 * application requirements.  This class will only be generated as
15
 * long as it does not already exist in the output directory.
16
 */
17
class User extends BaseUser implements ApiModelInterface {
18
19
	/**
20
	 */
21
	private static $serializer;
22
23
	/**
24
	 * @return UserSerializer
25
	 */
26
	public static function getSerializer() {
27
		if (self::$serializer === null) {
28
			self::$serializer = new UserSerializer();
29
		}
30
31
		return self::$serializer;
32
	}
33
34
	/**
35
	 * @param array $activity
36
	 */
37
	public function newActivity(array $activity) {
38
		$resolver = new OptionsResolver();
39
		$resolver->setRequired(array('verb', 'object'));
40
		$resolver->setDefined(array('target'));
41
		$resolver->setAllowedTypes('target', array('keeko\\framework\\model\\ActivityObjectInterface', 'keeko\\core\\model\\ActivityObject'));
42
		$resolver->setAllowedTypes('object', array('keeko\\framework\\model\\ActivityObjectInterface', 'keeko\\core\\model\\ActivityObject'));
43
		$options = $resolver->resolve($activity);
44
		$obj = new Activity();
45
		$obj->setActor($this);
46
		$obj->setVerb($options['verb']);
47
		$obj->setObject($this->getActivityObject($options['object']));
48
		if (isset($options['target'])) {
49
		    $obj->setTarget($this->getActivityObject($options['target']));
50
		}
51
		$obj->save();
52
	}
53
54
	/**
55
	 * @param ActivityObject $ao
56
	 * @return ActivityObject
57
	 */
58
	private function findActivityObject(ActivityObject $ao) {
59
		$q = ActivityObjectQuery::create()->filterByClassName($ao->getClassName())->filterByType($ao->getType())->filterByReferenceId($ao->getId());
60
		if (method_exists($ao, 'getVersion')) {
61
		    $version = $ao->getVersion();
62
		    if (!empty($version)) {
63
		        $q = $q->filterByVersion($version);
64
		    }
65
		}
66
		$result = $q->findOne();
67
		if ($result) {
68
		    $result->setDisplayName($ao->getDisplayName());
69
		    return $result;
70
		}
71
		return $ao;
72
	}
73
74
	/**
75
	 * @param mixed $obj
76
	 * @return ActivityObject
77
	 */
78
	private function getActivityObject($obj) {
79
		if ($obj instanceof ActivityObject) {
80
		    return $obj;
81
		}
82
		if ($obj instanceof ActivityObjectInterface) {
83
		    return $this->findActivityObject($obj->toActivityObject());
84
		}
85
	}
86
	
87
	/**
88
	 * Returns whether this user is a guest
89
	 *
90
	 * @return boolean
91
	 */
92
	public function isGuest() {
93
		return $this->getId() == -1;
94
	}
95
}
96