public function getGroupedTargets(?Application $application = null): array
    {
        $environments = $this->getEntityManager()
            ->getRepository(Environment::class)
            ->findAll();

        if ($application) {
            $findBy = ['application' => $application];
        } else {
            $findBy = [];
        }

        $targets = $this->findBy($findBy);

        usort($environments, $this->environmentSorter());
        usort($targets, $this->targetSorter());

        $sorted = [];
        foreach ($environments as $environment) {
            $sorted[$environment->id()] = [
                'environment' => $environment,
                'targets' => [],
            ];
        }

        foreach ($targets as $target) {
            $id = $target->environment()->id();
            $sorted[$id]['targets'][] = $target;
        }

        return array_filter($sorted, function ($e) {
            return count($e['targets']) !== 0;
        });
    }