Skip to content

Instantly share code, notes, and snippets.

@meinemitternacht
Last active October 26, 2017 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meinemitternacht/7838344bef9db1988bca9a8c8c4af165 to your computer and use it in GitHub Desktop.
Save meinemitternacht/7838344bef9db1988bca9a8c8c4af165 to your computer and use it in GitHub Desktop.
<?php
namespace DownlineAutomator\Models\Segments;
use DownlineAutomator\DLAHelperMethods;
use DownlineAutomator\Models\LogHelper;
use DownlineAutomator\Models\Segments\Containers\DynamicSegmentContainer;
use DownlineAutomator\Models\Segments\Containers\ISegmentContainer;
use DownlineAutomator\Models\Segments\Containers\ReportSegmentContainer;
use DownlineAutomator\Models\Segments\Containers\StaticSegmentContainer;
use DownlineAutomator\Models\Users\UserEntity;
use Monolog\Logger;
use Psr\Container\ContainerInterface;
class SegmentCollection
{
/**
* @var ContainerInterface
*/
private $container;
/**
* @var \mysqli
*/
protected $db;
/**
* @var Logger
*/
protected $log;
/**
* @var ISegmentContainer[]
*/
private $segmentContainers;
/**
* @param UserEntity $userEntity
* @param int|null $segmentType
* @return array|null
*/
public function fetchSegmentsByUser(UserEntity $userEntity, ?int $segmentType = null): ?array
{
if ($userEntity->getAccountID() === DLAHelperMethods::INVALID_MEMBER_ID) {
return null;
}
if ($segmentType !== null && !in_array($segmentType, Segment::VALID_TYPES)) {
return null;
}
$accountId = $userEntity->getAccountID();
$queryStr = "SELECT
uuid,
accountId,
type,
segmentName,
description
FROM segments
WHERE accountId = ?";
$stmt = $this->db->prepare($queryStr);
$stmt->bind_param(
'i',
$accountId
);
if (!$stmt->execute()) {
LogHelper::logQueryError($this->log, __METHOD__, $queryStr, $stmt);
$stmt->close();
return null;
}
$stmt->bind_result(
$uuid,
$_accountId,
$type,
$segmentName,
$description
);
$segments = [];
while ($stmt->fetch()) {
$segments[] = [
'uuid' => $uuid,
'accountId' => $_accountId,
'type' => $type,
'segmentName' => $segmentName,
'description' => $description
];
}
$stmt->close();
$segs = [];
$c = count($segments);
for ($i = 0; $i < $c; $i++) {
$segment = new Segment();
$segment->setUuid($segments[$i]['uuid']);
$segment->setAccountId($segments[$i]['accountId']);
$segment->setType($segments[$i]['type']);
$segment->setName($segments[$i]['segmentName']);
$segment->setDescription($segments[$i]['description']);
$segs[] = $segment;
}
var_dump($segs);
return $segs; //BREAKPOINT HERE, $segs = Array with data
}
}
<?php
namespace DownlineAutomator\Controllers;
use DownlineAutomator\DLAHelperMethods;
use DownlineAutomator\Models\BaseController;
use DownlineAutomator\Models\Messages;
use DownlineAutomator\Models\Request;
use DownlineAutomator\Models\Segments\Containers\ReportSegmentContainer;
use DownlineAutomator\Models\Segments\ReportSegmentOption;
use DownlineAutomator\Models\Segments\Segment;
use DownlineAutomator\Models\Segments\SegmentCollection;
use DownlineAutomator\Models\Users\UserCollection;
class Segments extends BaseController
{
public function renderSegmentsHome(): string
{
$userCollection = $this->container->get(UserCollection::class);
$segmentCollection = $this->container->get(SegmentCollection::class);
$userEntity = $userCollection->fetch(
DLAHelperMethods::getAccountId()
);
$reports = $segmentCollection->fetchSegmentsByUser(
$userEntity,
Segment::TYPE_REPORT
);
var_dump($reports);
return $this->twig->render( //BREAKPOINT HERE, $reports = NULL
'segments/home.html.twig',[
'segments' => [
'reports' => $reports //function call was here, moved out to view breakpoint
]
]);
}
}
@meinemitternacht
Copy link
Author

Output WITHOUT XDebug using var_dump() as indicated in revision 2:

`
array(1) {

  | [0]=>
  | object(DownlineAutomator\Models\Segments\Segment)#74 (5) {
  | ["uuid":"DownlineAutomator\Models\Segments\Segment":private]=>
  | string(36) "87346844-3a9e-494d-98bc-712a58edafd3"
  | ["accountId":"DownlineAutomator\Models\Segments\Segment":private]=>
  | int(863625)
  | ["type":"DownlineAutomator\Models\Segments\Segment":private]=>
  | int(0)
  | ["name":"DownlineAutomator\Models\Segments\Segment":private]=>
  | string(9) "Some Name"
  | ["description":"DownlineAutomator\Models\Segments\Segment":private]=>
  | string(21) "This is a description"
  | }
  | }
  | array(1) {
  | [0]=>
  | object(DownlineAutomator\Models\Segments\Segment)#74 (5) {
  | ["uuid":"DownlineAutomator\Models\Segments\Segment":private]=>
  | string(36) "87346844-3a9e-494d-98bc-712a58edafd3"
  | ["accountId":"DownlineAutomator\Models\Segments\Segment":private]=>
  | int(863625)
  | ["type":"DownlineAutomator\Models\Segments\Segment":private]=>
  | int(0)
  | ["name":"DownlineAutomator\Models\Segments\Segment":private]=>
  | string(9) "Some Name"
  | ["description":"DownlineAutomator\Models\Segments\Segment":private]=>
  | string(21) "This is a description"
  | }
  | }

`

@meinemitternacht
Copy link
Author

Output WITH Xdebug using var_dump() as indicated in revision 2:

`

  array(1) {
  [0]=>
  object(DownlineAutomator\Models\Segments\Segment)#74 (5) {
  ["uuid":"DownlineAutomator\Models\Segments\Segment":private]=>
  string(36) "87346844-3a9e-494d-98bc-712a58edafd3"
  ["accountId":"DownlineAutomator\Models\Segments\Segment":private]=>
  int(863625)
  ["type":"DownlineAutomator\Models\Segments\Segment":private]=>
  int(0)
  ["name":"DownlineAutomator\Models\Segments\Segment":private]=>
  string(9) "Some Name"
  ["description":"DownlineAutomator\Models\Segments\Segment":private]=>
  string(21) "This is a description"
  }
  }
  NULL

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment