-
Notifications
You must be signed in to change notification settings - Fork 5
/
FactionResolver.php
89 lines (70 loc) · 2.86 KB
/
FactionResolver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace AppBundle\GraphQL\Relay\Resolver;
use AppBundle\Entity\Faction;
use AppBundle\Entity\Repository\ShipRepository;
use Overblog\GraphQLBundle\Relay\Connection\Output\ConnectionBuilder;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
class FactionResolver implements ContainerAwareInterface
{
use ContainerAwareTrait;
public function resolveRebels()
{
$rebels = $this->getFactionByType(Faction::TYPE_REBELS);
return $rebels;
}
public function resolveEmpire()
{
$empire = $this->getFactionByType(Faction::TYPE_EMPIRE);
return $empire;
}
public function resolveFake()
{
$fake = $this->getFactionByType(Faction::TYPE_FAKE);
return $fake;
}
public function resolveShips(Faction $faction, $args)
{
//The old way
//$ships = $faction->getShips()->toArray();
//$connection = ConnectionBuilder::connectionFromArray($ships, $args);
//$connection->sliceSize = count($connection->edges);
//return $connection;
/** @var ShipRepository $repository */
$repository = $this->container
->get('doctrine.orm.default_entity_manager')
->getRepository('AppBundle:Ship');
$arrayLength = $repository->countAllByFactionId($faction->getId());
//--------------------------------------------------------------------------------------------------------------
//todo move in vendor ?
$beforeOffset = ConnectionBuilder::getOffsetWithDefault($args['before'], $arrayLength);
$afterOffset = ConnectionBuilder::getOffsetWithDefault($args['after'], -1);
$startOffset = max($afterOffset, -1) + 1;
$endOffset = min($beforeOffset, $arrayLength);
if (is_numeric($args['first'])) {
$endOffset = min($endOffset, $startOffset + $args['first']);
}
if (is_numeric($args['last'])) {
$startOffset = max($startOffset, $endOffset - $args['last']);
}
$offset = max($startOffset, 0);
$limit = $endOffset - $startOffset;
//--------------------------------------------------------------------------------------------------------------
$ships = $repository->retrieveShipsByFactionId($faction->getId(), $offset, $limit);
$connection = ConnectionBuilder::connectionFromArraySlice(
$ships,
$args,
[
'sliceStart' => $offset,
'arrayLength' => $arrayLength,
]
);
$connection->sliceSize = count($ships);
return $connection;
}
private function getFactionByType($type)
{
return $this->container->get('doctrine.orm.default_entity_manager')
->getRepository('AppBundle:Faction')->findOneBy(['type' => $type]);
}
}