Skip to content
Projects
Groups
Snippets
Help
Sign in / Register
Toggle navigation
Minds Backend - Engine
Project overview
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Locked Files
Issues
286
Merge Requests
38
CI / CD
Security & Compliance
Packages
Wiki
Snippets
Members
Collapse sidebar
Close sidebar
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Minds
Minds Backend - Engine
Compare Revisions
c966ecce90dd3617351f31b50839721bb0f0d7fe...702815694456b6fbc0f78c514708b9cb7fdcf17a
Source
702815694456b6fbc0f78c514708b9cb7fdcf17a
...
Target
c966ecce90dd3617351f31b50839721bb0f0d7fe
Compare
Commits (2)
Boost Campaign Objects -
#1201
· dd38b793
Guy Thouret
authored
15 minutes ago
dd38b793
Boost Campaign validate dates delegate -
#1201
· 70281569
Guy Thouret
authored
14 minutes ago
70281569
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
430 additions
and
22 deletions
+430
-22
Core/Boost/Delegates/ValidateCampaignDatesDelegate.php
0 → 100644
View file @
70281569
<?php
namespace
Minds\Core\Boost\Delegates
;
use
Minds\Core\Boost\Network\Campaign
;
use
Minds\Core\Boost\Network\CampaignException
;
class
ValidateCampaignDatesDelegate
{
/**
* @param Campaign $campaign
* @return Campaign
* @throws CampaignException
*/
public
function
onCreate
(
Campaign
$campaign
)
{
$start
=
$this
->
normaliseStartTime
(
$campaign
->
getStart
());
$end
=
$this
->
normaliseEndTime
(
$campaign
->
getEnd
());
$this
->
validateStartTime
(
$start
);
$this
->
validateEndTime
(
$end
);
$this
->
validateStartAgainstEnd
(
$start
,
$end
);
return
$campaign
->
setStart
(
$start
)
->
setEnd
(
$end
);
}
/**
* @param Campaign $campaign
* @param Campaign $campaignRef
* @return Campaign
* @throws CampaignException
*/
public
function
onUpdate
(
Campaign
$campaign
,
Campaign
$campaignRef
)
{
// TODO: Ensure date updates from ref are valid against original campaign budget, etc.
$start
=
$this
->
normaliseStartTime
(
$campaignRef
->
getStart
());
$end
=
$this
->
normaliseEndTime
(
$campaignRef
->
getEnd
());
$this
->
validateStartTime
(
$start
);
$this
->
validateEndTime
(
$end
);
$this
->
validateStartAgainstEnd
(
$start
,
$end
);
if
(
!
$campaign
->
hasStarted
())
{
$campaign
->
setStart
(
$start
);
}
if
(
!
$campaign
->
hasFinished
()
&&
$campaign
->
getEnd
()
<
$end
)
{
$campaign
->
setEnd
(
$end
);
}
return
$campaign
;
}
private
function
normaliseStartTime
(
int
$startTime
)
:
int
{
return
strtotime
(
date
(
'Y-m-d'
,
$startTime
/
1000
)
.
' 00:00:00'
)
*
1000
;
}
private
function
normaliseEndTime
(
int
$endTime
)
:
int
{
return
strtotime
(
date
(
'Y-m-d'
,
$endTime
/
1000
)
.
' 23:59:59'
)
*
1000
;
}
private
function
validateStartTime
(
int
$startTime
)
:
void
{
if
(
$startTime
<=
0
)
{
throw
new
CampaignException
(
'Campaign should have a start date'
);
}
$today
=
strtotime
(
date
(
'Y-m-d'
)
.
' 00:00:00'
)
*
1000
;
if
(
$startTime
<
$today
)
{
throw
new
CampaignException
(
'Campaign start should not be in the past'
);
}
}
private
function
validateEndTime
(
int
$endTime
)
:
void
{
if
(
$endTime
<=
0
)
{
throw
new
CampaignException
(
'Campaign should have an end date'
);
}
}
private
function
validateStartAgainstEnd
(
int
$start
,
int
$end
)
:
void
{
if
(
$start
>=
$end
)
{
throw
new
CampaignException
(
'Campaign end before starting'
);
}
$startPlusOneMonth
=
strtotime
(
'+1 month'
,
$start
/
1000
)
*
1000
;
if
(
$startPlusOneMonth
<
$end
)
{
throw
new
CampaignException
(
'Campaign must not be longer than 1 month'
);
}
}
}
This diff is collapsed.
Core/Boost/Network/Boost.php
View file @
70281569
...
...
@@ -53,6 +53,8 @@ use Minds\Traits\MagicAttributes;
* @method Boost setRejectedReason(int $reason)
* @method string getChecksum()
* @method Boost setChecksum(string $checksum)
* @method string getBoostType()
* @method Boost setBoostType()
*/
class
Boost
{
...
...
@@ -70,71 +72,77 @@ class Boost
const
RATING_SAFE
=
1
;
const
RATING_OPEN
=
2
;
const
BOOST_TYPE_NOW
=
'now'
;
const
BOOST_TYPE_CAMPAIGN
=
'campaign'
;
/** @var int $guid */
pr
ivate
$guid
;
pr
otected
$guid
;
/** @var int $entityGuid */
pr
ivate
$entityGuid
;
pr
otected
$entityGuid
;
/** @var Entity $entity */
pr
ivate
$entity
;
pr
otected
$entity
;
/** @var double $bid */
pr
ivate
$bid
;
pr
otected
$bid
;
/** @var string $bidType */
pr
ivate
$bidType
;
pr
otected
$bidType
;
/** @var int $impressions */
pr
ivate
$impressions
;
pr
otected
$impressions
;
/** @var int $impressionsMet */
pr
ivate
$impressionsMet
;
pr
otected
$impressionsMet
;
/** @var int $ownerGuid */
pr
ivate
$ownerGuid
;
pr
otected
$ownerGuid
;
/** @var User $owner */
pr
ivate
$owner
;
pr
otected
$owner
;
/** @var int $createdTimestamp */
pr
ivate
$createdTimestamp
;
pr
otected
$createdTimestamp
;
/** @var int $reviewedTimestamp */
pr
ivate
$reviewedTimestamp
;
pr
otected
$reviewedTimestamp
;
/** @var int $rejectedTimestamp */
pr
ivate
$rejectedTimestamp
;
pr
otected
$rejectedTimestamp
;
/** @var int $revokedTimestamp */
pr
ivate
$revokedTimestamp
;
pr
otected
$revokedTimestamp
;
/** @var int $completedTimestamp */
pr
ivate
$completedTimestamp
;
pr
otected
$completedTimestamp
;
/** @var string $transactionId */
pr
ivate
$transactionId
;
pr
otected
$transactionId
;
/** @var string $type */
pr
ivate
$type
=
'newsfeed'
;
pr
otected
$type
=
'newsfeed'
;
/** @var bool $priority */
pr
ivate
$priority
=
false
;
pr
otected
$priority
=
false
;
/** @var int $rating */
pr
ivate
$rating
;
pr
otected
$rating
;
/** @var array $tags */
pr
ivate
$tags
=
[];
pr
otected
$tags
=
[];
/** @var array $nsfw */
pr
ivate
$nsfw
=
[];
pr
otected
$nsfw
=
[];
/** @var int $rejectedReason */
pr
ivate
$rejectedReason
=
-
1
;
pr
otected
$rejectedReason
=
-
1
;
/** @var string $checksum */
private
$checksum
;
protected
$checksum
;
/** @var string $boostType */
protected
$boostType
=
self
::
BOOST_TYPE_NOW
;
/**
* Return the state
...
...
@@ -193,6 +201,7 @@ class Boost
'checksum'
=>
$this
->
checksum
,
'state'
=>
$this
->
getState
(),
'transaction_id'
=>
$this
->
transactionId
,
'boost_type'
=>
$this
->
boostType
,
];
}
...
...
This diff is collapsed.
Core/Boost/Network/Campaign.php
0 → 100644
View file @
70281569
<?php
namespace
Minds\Core\Boost\Network
;
use
Minds\Entities\Entity
;
use
Minds\Entities\User
;
use
Minds\Traits\MagicAttributes
;
/**
* Class Campaign
* @package Minds\Core\Boost\Network
* @method Campaign setGuid(int $guid)
* @method int getGuid()
* @method Campaign setEntityGuid(int $entityGuid)
* @method int getEntityGuid()
* @method Campaign setEntity($entity)
* @method Entity getEntity()
* @method Campaign setBid(double $bid)
* @method double getBid()
* @method Campaign setBidType(string $bidType)
* @method string getBidType()
* @method Campaign setImpressions(int $impressions)
* @method int getImpressions()
* @method Campaign setImpressionsMet(int $impressions)
* @method int getImpressionsMet()
* @method Campaign setOwnerGuid(int $ownerGuid)
* @method int getOwnerGuid()
* @method Campaign setOwner(User $owner)
* @method User getOwner()
* @method int getCreatedTimestamp()
* @method Campaign setCreatedTimestamp(int $ts)
* @method int getReviewedTimestamp()
* @method Campaign setReviewedTimestamp(int $ts)
* @method int getRejectedTimestamp()
* @method Campaign setRejectedTimestamp(int $ts)
* @method int getRevokedTimestamp()
* @method Campaign setRevokedTimestamp(int $ts)
* @method int getCompletedTimestamp()
* @method Campaign setCompletedTimestamp(int $ts)
* @method string getTransactionId()
* @method Campaign setTransactionId(string $transactionId)
* @method string getType()
* @method Campaign setType(string $value)
* @method bool getPriority()
* @method Campaign setPriority(bool $priority)
* @method int getRating()
* @method Campaign setRating(int $rating)
* @method array getTags()
* @method Campaign setTags(array $value)
* @method array getNsfw()
* @method Campaign setNsfw(array $nsfw)
* @method int getRejectedReason()
* @method Campaign setRejectedReason(int $reason)
* @method string getChecksum()
* @method Campaign setChecksum(string $checksum)
* @method string getBoostType()
* @method Campaign setBoostType()
* @method string getName()
* @method Campaign setName(string $name)
* @method int getStart()
* @method Campaign setStart(int $start)
* @method int getEnd()
* @method Campaign setEnd(int $end)
* @method int getBudget()
* @method Campaign setBudget(int $budget)
* @method int getDailyCap()
* @method Campaign setDailyCap(int $dailyCap)
*/
class
Campaign
extends
Boost
implements
\JsonSerializable
{
use
MagicAttributes
;
const
STATE_PENDING
=
'pending'
;
/** @var string $boostType */
protected
$boostType
=
self
::
BOOST_TYPE_CAMPAIGN
;
/** @var string $name */
protected
$name
;
/** @var int $start */
protected
$start
;
/** @var int $end */
protected
$end
;
/** @var int $budget */
protected
$budget
;
/** @var int $dailyCap */
protected
$dailyCap
;
public
function
export
(
$fields
=
[])
:
array
{
$boostExport
=
parent
::
export
(
$fields
);
$campaignExport
=
[
'name'
=>
$this
->
name
,
'@start'
=>
$this
->
start
,
'@end'
=>
$this
->
end
,
'budget'
=>
$this
->
budget
,
'daily_cap'
=>
$this
->
dailyCap
,
'delivery_status'
=>
$this
->
getDeliveryStatus
(),
'cpm'
=>
$this
->
cpm
()
];
return
array_merge
(
$boostExport
,
$campaignExport
);
}
public
function
getDeliveryStatus
()
:
string
{
if
(
$this
->
completedTimestamp
)
{
return
self
::
STATE_COMPLETED
;
}
elseif
(
$this
->
rejectedTimestamp
)
{
return
self
::
STATE_REJECTED
;
}
elseif
(
$this
->
revokedTimestamp
)
{
return
self
::
STATE_REVOKED
;
}
elseif
(
$this
->
reviewedTimestamp
)
{
return
self
::
STATE_APPROVED
;
}
elseif
(
$this
->
createdTimestamp
)
{
return
self
::
STATE_CREATED
;
}
return
self
::
STATE_PENDING
;
}
public
function
cpm
()
:
float
{
if
(
!
$this
->
impressions
||
$this
->
impressions
===
0
)
{
return
0
;
}
return
(
$this
->
budget
/
$this
->
impressions
)
*
1000
;
}
public
function
isDelivering
()
:
bool
{
return
$this
->
getDeliveryStatus
()
===
self
::
STATE_APPROVED
;
}
public
function
shouldBeStarted
(
int
$now
)
:
bool
{
$isCreated
=
$this
->
getDeliveryStatus
()
===
self
::
STATE_CREATED
;
$started
=
$now
>=
$this
->
getStart
()
&&
$now
<
$this
->
getEnd
();
return
$isCreated
&&
$started
;
}
public
function
shouldBeCompleted
(
int
$now
)
:
bool
{
$isDelivering
=
$this
->
isDelivering
();
$ended
=
$now
>=
$this
->
getEnd
();
$fulfilled
=
$this
->
getImpressionsMet
()
>=
$this
->
getImpressions
();
return
$isDelivering
&&
(
$ended
||
$fulfilled
);
}
public
function
hasStarted
()
:
bool
{
return
!
in_array
(
$this
->
getDeliveryStatus
(),
[
self
::
STATE_PENDING
,
self
::
STATE_CREATED
],
true
);
}
public
function
hasFinished
()
:
bool
{
return
in_array
(
$this
->
getDeliveryStatus
(),
[
self
::
STATE_COMPLETED
,
self
::
STATE_REJECTED
,
self
::
STATE_REVOKED
,
],
false
);
}
/**
* Specify data which should be serialized to JSON
* @link https://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
public
function
jsonSerialize
()
{
return
$this
->
export
();
}
public
function
getData
()
:
array
{
$data
=
$this
->
export
();
/* TODO: Filter data here */
return
$data
;
}
}
This diff is collapsed.
Core/Boost/Network/CampaignException.php
0 → 100644
View file @
70281569
<?php
namespace
Minds\Core\Boost\Network
;
use
Exception
;
class
CampaignException
extends
Exception
{
}
This diff is collapsed.
Spec/Core/Boost/Delegates/ValidateCampaignDatesDelegateSpec.php
0 → 100644
View file @
70281569
<?php
namespace
Spec\Minds\Core\Boost\Delegates
;
use
Minds\Core\Boost\Network\Campaign
;
use
Minds\Core\Boost\Network\CampaignException
;
use
Minds\Core\Boost\Delegates\ValidateCampaignDatesDelegate
;
use
PhpSpec\ObjectBehavior
;
class
ValidateCampaignDatesDelegateSpec
extends
ObjectBehavior
{
public
function
it_is_initializable
()
{
$this
->
shouldHaveType
(
ValidateCampaignDatesDelegate
::
class
);
}
public
function
it_should_reject_start_date_in_past_on_create
(
Campaign
$campaign
)
{
$campaign
->
getStart
()
->
willReturn
(
strtotime
(
'-1 day'
)
*
1000
);
$campaign
->
getEnd
()
->
willReturn
(
strtotime
(
'+3 days'
)
*
1000
);
$this
->
shouldThrow
(
CampaignException
::
class
)
->
during
(
'onCreate'
,
[
$campaign
]);
}
public
function
it_should_reject_end_date_before_start_date_on_create
(
Campaign
$campaign
)
{
$campaign
->
getStart
()
->
willReturn
(
strtotime
(
'+2 days'
)
*
1000
);
$campaign
->
getEnd
()
->
willReturn
(
strtotime
(
'+1 days'
)
*
1000
);
$this
->
shouldThrow
(
CampaignException
::
class
)
->
during
(
'onCreate'
,
[
$campaign
]);
}
public
function
it_should_adjust_start_and_end_to_first_and_last_timestamps_of_days_on_create
(
Campaign
$campaign
)
{
$campaign
->
getStart
()
->
willReturn
(
strtotime
(
'today 12:01:31'
)
*
1000
);
$campaign
->
getEnd
()
->
willReturn
(
strtotime
(
'+1 days 17:32:05'
)
*
1000
);
$campaign
->
setStart
(
strtotime
(
'today 00:00:00'
)
*
1000
)
->
shouldBeCalled
()
->
willReturn
(
$campaign
);
$campaign
->
setEnd
(
strtotime
(
'+1 days 23:59:59'
)
*
1000
)
->
shouldBeCalled
()
->
willReturn
(
$campaign
);
$this
->
onCreate
(
$campaign
);
}
public
function
it_should_accept_valid_days_on_create
(
Campaign
$campaign
)
{
$campaign
->
getStart
()
->
willReturn
(
strtotime
(
'+2 days'
)
*
1000
);
$campaign
->
getEnd
()
->
willReturn
(
strtotime
(
'+5 days'
)
*
1000
);
$campaign
->
setStart
(
strtotime
(
'+2 days 00:00:00'
)
*
1000
)
->
shouldBeCalled
()
->
willReturn
(
$campaign
);
$campaign
->
setEnd
(
strtotime
(
'+5 days 23:59:59'
)
*
1000
)
->
shouldBeCalled
()
->
willReturn
(
$campaign
);
$this
->
onCreate
(
$campaign
);
}
public
function
it_should_reject_campaign_longer_than_one_month
(
Campaign
$campaign
)
{
$campaign
->
getStart
()
->
willReturn
(
strtotime
(
'+2 days'
)
*
1000
);
$campaign
->
getEnd
()
->
willReturn
(
strtotime
(
'+34 days'
)
*
1000
);
$this
->
shouldThrow
(
CampaignException
::
class
)
->
during
(
'onCreate'
,
[
$campaign
]);
}
/* public function it_should_validate_dates_are_valid_against_campaign_budget_on_update()
{
// TODO: Not Implemented yet
}*/
public
function
it_should_not_change_start_date_if_campaign_has_started_on_update
(
Campaign
$campaign
,
Campaign
$campaignRef
)
{
$campaign
->
hasStarted
()
->
shouldBeCalled
()
->
willReturn
(
true
);
$campaign
->
hasFinished
()
->
shouldBeCalled
()
->
willReturn
(
true
);
$campaignRef
->
getStart
()
->
willReturn
(
strtotime
(
'+2 days'
)
*
1000
);
$campaignRef
->
getEnd
()
->
willReturn
(
strtotime
(
'+5 days'
)
*
1000
);
$campaign
->
getStart
()
->
willReturn
(
strtotime
(
'+2 days'
)
*
1000
);
$campaign
->
getEnd
()
->
willReturn
(
strtotime
(
'+5 days'
)
*
1000
);
$this
->
onUpdate
(
$campaign
,
$campaignRef
);
}
public
function
it_should_change_start_date_if_campaign_has_not_started_on_update
(
Campaign
$campaign
,
Campaign
$campaignRef
)
{
$campaign
->
hasStarted
()
->
shouldBeCalled
()
->
willReturn
(
false
);
$campaign
->
hasFinished
()
->
shouldBeCalled
()
->
willReturn
(
false
);
$campaignRef
->
getStart
()
->
willReturn
(
strtotime
(
'+2 days'
)
*
1000
);
$campaignRef
->
getEnd
()
->
willReturn
(
strtotime
(
'+5 days'
)
*
1000
);
$campaign
->
getStart
()
->
willReturn
(
strtotime
(
'+2 days'
)
*
1000
);
$campaign
->
getEnd
()
->
willReturn
(
strtotime
(
'+5 days'
)
*
1000
);
$campaign
->
setStart
(
strtotime
(
'+2 days 00:00:00'
)
*
1000
)
->
shouldBeCalled
()
->
willReturn
(
$campaign
);
$campaign
->
setEnd
(
strtotime
(
'+5 days 23:59:59'
)
*
1000
)
->
shouldBeCalled
();
$this
->
onUpdate
(
$campaign
,
$campaignRef
);
}
public
function
it_should_only_change_end_date_if_campaign_hasnt_finished_on_update
(
Campaign
$campaign
,
Campaign
$campaignRef
)
{
$campaign
->
hasStarted
()
->
shouldBeCalled
()
->
willReturn
(
true
);
$campaign
->
hasFinished
()
->
shouldBeCalled
()
->
willReturn
(
false
);
$campaignRef
->
getStart
()
->
willReturn
(
strtotime
(
'+2 days'
)
*
1000
);
$campaignRef
->
getEnd
()
->
willReturn
(
strtotime
(
'+7 days'
)
*
1000
);
$campaign
->
getStart
()
->
willReturn
(
strtotime
(
'+2 days'
)
*
1000
);
$campaign
->
getEnd
()
->
willReturn
(
strtotime
(
'+5 days'
)
*
1000
);
$campaign
->
setEnd
(
strtotime
(
'+7 days 23:59:59'
)
*
1000
)
->
shouldBeCalled
();
$this
->
onUpdate
(
$campaign
,
$campaignRef
);
}
}
This diff is collapsed.