...
 
Commits (3)
<?php
/**
* preview
* @author edgebal
*/
namespace Minds\Controllers\api\v2\boost\campaigns;
use Minds\Api\Api;
use Minds\Core\Boost\Campaigns\Campaign;
use Minds\Core\Boost\Campaigns\Stats;
use Minds\Core\Di\Di;
use Minds\Interfaces;
use Minds\Api\Factory;
class preview implements Interfaces\Api
class preview extends Api
{
/**
* Equivalent to HTTP GET method
* @param array $pages
* @return mixed|null
*/
public function get($pages)
{
return Factory::response([]);
}
/**
* Equivalent to HTTP POST method
* @param array $pages
* @return mixed|null
*/
public function post($pages)
public function post($pages): void
{
$campaign = new Campaign();
$campaign
$campaign = (new Campaign())
->setType($_POST['type'] ?? '')
->setEntityUrns($_POST['entity_urns'] ?? [])
->setBudgetType($_POST['budget_type'] ?? '')
->setHashtags($_POST['hashtags'] ?? [])
->setStart((int) ($_POST['start'] ?? 0))
->setEnd((int) ($_POST['end'] ?? 0))
->setBudget((float) ($_POST['budget'] ?? 0));
->setStart((int)($_POST['start'] ?? 0))
->setEnd((int)($_POST['end'] ?? 0))
->setBudget((float)($_POST['budget'] ?? 0))
->setImpressions($_POST['impressions']);
/** @var Stats $statsManager */
$statsManager = Di::_()->get('Boost\Campaigns\Stats');
$stats = $statsManager->get($campaign);
return Factory::response([
'preview' => [
'cannot_fulfill_daily' => $stats['cannot_fulfill_daily'],
]
]);
}
/**
* Equivalent to HTTP PUT method
* @param array $pages
* @return mixed|null
*/
public function put($pages)
{
return Factory::response([]);
}
/**
* Equivalent to HTTP DELETE method
* @param array $pages
* @return mixed|null
*/
public function delete($pages)
{
return Factory::response([]);
$this->send($statsManager->setCampaign($campaign)->getAll());
}
}
......@@ -7,6 +7,7 @@ use Minds\Common\Repository\Response;
use Minds\Common\Urn;
use Minds\Core\Boost\Campaigns\Payments\Payment;
use Minds\Core\Boost\Campaigns\Payments\Repository as PaymentsRepository;
use Minds\Core\Boost\Checksum;
use Minds\Core\Queue\Client as QueueClient;
use Minds\Core\Queue\Interfaces\QueueClient as QueueClientInterface;
use Minds\Entities\User;
......@@ -202,7 +203,10 @@ class Manager
throw new CampaignException('Invalid campaign type');
}
// TODO: Validate based on data
/** TODO: Checksum Verification */
//$guid = (new Urn($campaign->getEntityUrns()[0]))->getNss();
//$checksum = (new Checksum())->setGuid($campaign->getGuid())->setEntity($guid)->generate();
//if (!$campaign->getChecksum() || ($campaign->getChecksum() !== $checksum)) {
if (!$campaign->getChecksum()) {
throw new CampaignException('Invalid checksum value');
}
......
......@@ -2,16 +2,48 @@
namespace Minds\Core\Boost\Campaigns;
use Minds\Core\Analytics\EntityCentric\BoostViewsDaily;
use Minds\Core\Time;
class Stats
{
/** @var Campaign */
protected $campaign;
/** @var BoostViewsDaily */
protected $boostViewsDaily;
public function __construct(BoostViewsDaily $boostViewsDaily = null)
{
$this->boostViewsDaily = $boostViewsDaily ?: new BoostViewsDaily();
}
/**
* @param Campaign $campaign
* @return Stats
*/
public function setCampaign(Campaign $campaign): self
{
$this->campaign = $campaign;
return $this;
}
/**
* @return array
*/
public function get(Campaign $campaign)
public function getAll(): array
{
/* TODO: Evaluate the campaign targetting parameters against our data */
$campaignDurationDays = ($this->campaign->getEnd() - $this->campaign->getStart()) / Time::ONE_DAY_MS;
$campaignViewsPerDayReq = ($campaignDurationDays > 0) ? $this->campaign->getImpressions() / $campaignDurationDays : 0;
$globalViewsPerDay = $this->boostViewsDaily->getAvg();
return [
'cannot_fulfill_daily' => false,
'canBeDelivered' => ($campaignViewsPerDayReq < $globalViewsPerDay),
'durationDays' => $campaignDurationDays,
'viewsPerDayRequested' => $campaignViewsPerDayReq,
'globalViewsPerDay' => $globalViewsPerDay
];
}
}
......@@ -157,6 +157,8 @@ class ManagerSpec extends ObjectBehavior
$campaign->getOwnerGuid()->shouldBeCalled()->willReturn(1234);
$campaign->getName()->shouldBeCalled()->willReturn('Test Campaign');
$campaign->getType()->shouldBeCalled()->willReturn('newsfeed');
//$campaign->getEntityUrns()->shouldBeCalled()->willReturn(['urn:activity:12345']);
//$campaign->getGuid()->shouldBeCalled()->willReturn(12345);
$campaign->getChecksum()->shouldBeCalled()->willReturn(null);
$this->shouldThrow(CampaignException::class)->duringCreateCampaign($campaign);
}
......
......@@ -2,21 +2,66 @@
namespace Spec\Minds\Core\Boost\Campaigns;
use Minds\Core\Analytics\EntityCentric\BoostViewsDaily;
use Minds\Core\Boost\Campaigns\Campaign;
use Minds\Core\Boost\Campaigns\Stats;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class StatsSpec extends ObjectBehavior
{
/** @var BoostViewsDaily */
protected $boostViewsDaily;
public function it_is_initializable()
{
$this->shouldHaveType(Stats::class);
}
public function it_should_get_stats(Campaign $campaign)
public function let(BoostViewsDaily $boostViewsDaily)
{
$this->beConstructedWith($boostViewsDaily);
$this->boostViewsDaily = $boostViewsDaily;
}
public function it_should_set_campaign(Campaign $campaign)
{
$this->setCampaign($campaign)->shouldReturn($this);
}
public function it_should_get_all_stats_with_can_be_delivered_false(Campaign $campaign)
{
$this->get($campaign)->shouldReturn([
'cannot_fulfill_daily' => false,
]);
$stats = [
'canBeDelivered' => false,
'durationDays' => 5,
'viewsPerDayRequested' => 50000,
'globalViewsPerDay' => 10000
];
$this->setCampaign($campaign);
$campaign->getEnd()->shouldBeCalled()->willReturn(1570924800000);
$campaign->getStart()->shouldBeCalled()->willReturn(1570492800000);
$campaign->getImpressions()->shouldBeCalled()->willReturn(250000);
$this->boostViewsDaily->getAvg()->shouldBeCalled()->willReturn(10000);
$this->getAll()->shouldHaveKeyWithValue('canBeDelivered', false);
}
public function it_should_get_all_stats(Campaign $campaign)
{
$stats = [
'canBeDelivered' => true,
'durationDays' => 5,
'viewsPerDayRequested' => 1000,
'globalViewsPerDay' => 10000
];
$this->setCampaign($campaign);
$campaign->getEnd()->shouldBeCalled()->willReturn(1570924800000);
$campaign->getStart()->shouldBeCalled()->willReturn(1570492800000);
$campaign->getImpressions()->shouldBeCalled()->willReturn(5000);
$this->boostViewsDaily->getAvg()->shouldBeCalled()->willReturn(10000);
$this->getAll()->shouldHaveKeyWithValue('canBeDelivered', true);
}
}