...
 
Commits (2)
......@@ -5,10 +5,20 @@ namespace Minds\Core\Blogs\Delegates;
use Minds\Core\Entities\Propagator\Properties;
use Minds\Entities\Activity;
/**
* Class PropagateProperties
* @package Minds\Core\Blogs\Delegates
*/
class PropagateProperties extends Properties
{
protected $actsOnSubtype = 'blog';
/**
* Propagate Entity properties to activity
* @param $from
* @param Activity $to
* @return Activity
*/
public function toActivity($from, Activity $to): Activity
{
if ($this->valueHasChanged($from->getTitle(), $to->get('title'))) {
......@@ -31,6 +41,12 @@ class PropagateProperties extends Properties
return $to;
}
/**
* Propagate activity properties to entity
* @param Activity $from
* @param $to
* @return mixed
*/
public function fromActivity(Activity $from, $to)
{
return $to;
......
......@@ -5,8 +5,18 @@ namespace Minds\Core\Entities\Delegates;
use Minds\Core\Entities\Propagator\Properties;
use Minds\Entities\Activity;
/**
* Class PropagateProperties
* @package Minds\Core\Entities\Delegates
*/
class PropagateProperties extends Properties
{
/**
* Propagate Entity properties to activity
* @param $from
* @param Activity $to
* @return Activity
*/
public function toActivity($from, Activity $to): Activity
{
if ($this->valueHasChanged($from->getNsfw(), $to->getNsfw())) {
......@@ -20,6 +30,12 @@ class PropagateProperties extends Properties
return $to;
}
/**
* Propagate activity properties to entity
* @param Activity $from
* @param $to
* @return mixed
*/
public function fromActivity(Activity $from, $to)
{
if ($this->valueHasChanged($from->getNsfw(), $to->getNsfw())) {
......
......@@ -10,6 +10,10 @@ use Minds\Core\EntitiesBuilder;
use Minds\Entities\Activity;
use Minds\Core;
/**
* Class PropagateProperties
* @package Minds\Core\Entities
*/
class PropagateProperties
{
/** @var Properties[] */
......@@ -23,6 +27,12 @@ class PropagateProperties
/** @var bool */
private $changed = false;
/**
* PropagateProperties constructor.
* @param Call|null $db
* @param Save|null $save
* @param EntitiesBuilder|null $entitiesBuilder
*/
public function __construct(Call $db = null, Save $save = null, EntitiesBuilder $entitiesBuilder = null)
{
$this->db = $db ?? new Call('entities_by_time');
......@@ -31,9 +41,12 @@ class PropagateProperties
$this->registerPropagators();
}
/**
* Register PropagateProperties classes
* @throws \Exception
*/
protected function registerPropagators(): void
{
/* Register PropertyPropagator classes here */
$this->addPropagator(Core\Blogs\Delegates\PropagateProperties::class);
$this->addPropagator(Core\Feeds\Delegates\PropagateProperties::class);
$this->addPropagator(Core\Media\Delegates\PropagateProperties::class);
......@@ -41,11 +54,16 @@ class PropagateProperties
$this->addPropagator(Core\Permissions\Delegates\PropagateProperties::class);
}
public function clearPropogators()
public function clearPropogators(): void
{
$this->propagators = [];
}
/**
* Add a propagator to be called in the chain
* @param string $class
* @throws \Exception
*/
protected function addPropagator(string $class): void
{
$obj = new $class();
......@@ -56,6 +74,10 @@ class PropagateProperties
$this->propagators[] = $obj;
}
/**
* Propagate the properties from the passed entity
* @param $entity
*/
public function from($entity): void
{
if ($entity instanceof Activity) {
......@@ -65,6 +87,11 @@ class PropagateProperties
}
}
/**
* Propagate properties from an Activity to to it's attachment
* @param Activity $activity
* @throws \Minds\Exceptions\StopEventException
*/
protected function fromActivity(Activity $activity): void
{
$this->changed = false;
......@@ -75,7 +102,7 @@ class PropagateProperties
foreach ($this->propagators as $propagator) {
if ($propagator->willActOnEntity($attachment)) {
$propagator->fromActivity($activity, $attachment);
$attachment = $propagator->fromActivity($activity, $attachment);
$this->changed |= $propagator->changed();
}
}
......@@ -85,6 +112,11 @@ class PropagateProperties
}
}
/**
* Propagate properties from an Entity to it's activities
* @param $entity
* @throws \Minds\Exceptions\StopEventException
*/
protected function toActivities($entity): void
{
$activities = $this->getActivitiesForEntity($entity->getGuid());
......@@ -97,6 +129,7 @@ class PropagateProperties
}
/**
* Get activities for an entity
* @param string $entityGuid
* @return Activity[]
*/
......@@ -111,12 +144,17 @@ class PropagateProperties
return $activities;
}
/**
* Propagate properties from and entity to an activity
* @param $entity
* @param Activity $activity
*/
public function propagateToActivity($entity, Activity &$activity): void
{
$this->changed = false;
foreach ($this->propagators as $propagator) {
if ($propagator->willActOnEntity($entity)) {
$propagator->toActivity($entity, $activity);
$activity = $propagator->toActivity($entity, $activity);
$this->changed |= $propagator->changed();
}
}
......
......@@ -4,22 +4,45 @@ namespace Minds\Core\Entities\Propagator;
use Minds\Entities\Activity;
/**
* Properties class that all PropagateProperties delegates should inherit
* @package Minds\Core\Entities\Propagator
*/
abstract class Properties
{
/**
* @var string
*/
protected $actsOnType = 'any';
/**
* @var string
*/
protected $actsOnSubtype = 'any';
/**
* @var bool
*/
protected $changed = false;
/**
* @return string
*/
public function actsOnType(): string
{
return $this->actsOnType;
}
/**
* @return string
*/
public function actsOnSubType(): string
{
return $this->actsOnSubtype;
}
/**
* @param $entity
* @return bool
*/
public function willActOnEntity($entity): bool
{
if ($this->actsOnType === 'any' || $this->actsOnType === $entity->getType()) {
......@@ -29,6 +52,11 @@ abstract class Properties
return false;
}
/**
* @param $from
* @param $to
* @return bool
*/
protected function valueHasChanged($from, $to): bool
{
$changed = $from !== $to;
......@@ -36,6 +64,9 @@ abstract class Properties
return $changed;
}
/**
* @return bool
*/
public function changed(): bool
{
return $this->changed;
......
......@@ -5,8 +5,18 @@ namespace Minds\Core\Feeds\Delegates;
use Minds\Core\Entities\Propagator\Properties;
use Minds\Entities\Activity;
/**
* Class PropagateProperties
* @package Minds\Core\Feeds\Delegates
*/
class PropagateProperties extends Properties
{
/**
* Propagate Entity properties to activity
* @param $from
* @param Activity $to
* @return Activity
*/
public function toActivity($from, Activity $to): Activity
{
if ($this->valueHasChanged((int)$from->getModeratorGuid(), (int)$to->getModeratorGuid())) {
......@@ -20,6 +30,12 @@ class PropagateProperties extends Properties
return $to;
}
/**
* Propagate activity properties to entity
* @param Activity $from
* @param $to
* @return mixed
*/
public function fromActivity(Activity $from, $to)
{
if ($this->valueHasChanged((int)$from->getModeratorGuid(), (int)$to->getModeratorGuid())) {
......
......@@ -5,11 +5,21 @@ namespace Minds\Core\Media\Delegates;
use Minds\Core\Entities\Propagator\Properties;
use Minds\Entities\Activity;
/**
* Class PropagateProperties
* @package Minds\Core\Media\Delegates
*/
class PropagateProperties extends Properties
{
protected $actsOnType = 'object';
protected $actsOnSubtype = ['image', 'video'];
/**
* Propagate Entity properties to activity
* @param $from
* @param Activity $to
* @return Activity
*/
public function toActivity($from, Activity $to): Activity
{
if ($this->valueHasChanged($from->title, $to->getMessage())) {
......@@ -25,6 +35,12 @@ class PropagateProperties extends Properties
return $to;
}
/**
* Propagate activity properties to entity
* @param Activity $from
* @param $to
* @return mixed
*/
public function fromActivity(Activity $from, $to)
{
return $to;
......
......@@ -5,8 +5,18 @@ namespace Minds\Core\Permissions\Delegates;
use Minds\Core\Entities\Propagator\Properties;
use Minds\Entities\Activity;
/**
* Class PropagateProperties
* @package Minds\Core\Permissions\Delegates
*/
class PropagateProperties extends Properties
{
/**
* Propagate Entity properties to activity
* @param $from
* @param Activity $to
* @return Activity
*/
public function toActivity($from, Activity $to): Activity
{
if ($this->valueHasChanged($from->getAllowComments(), $to->getAllowComments())) {
......@@ -16,6 +26,12 @@ class PropagateProperties extends Properties
return $to;
}
/**
* Propagate activity properties to entity
* @param Activity $from
* @param $to
* @return mixed
*/
public function fromActivity(Activity $from, $to)
{
if ($this->valueHasChanged($from->getAllowComments(), $to->getAllowComments())) {
......