From perimeters names, find navitia instances ids for add permissions

@param array $coverages
498285
private function getInstancesIdsFromExternalsCoveragesIds(\Traversable $perimeters)
    {
        $userPerimeterNavitiaIds = array();
        $instances = $this->getInstances();

        foreach ($perimeters as $perimeter) {
            $userPerimeterNavitiaId = array_search($perimeter->getExternalCoverageId(), $instances);
            if ($userPerimeterNavitiaId) {
                $userPerimeterNavitiaIds[] = array_search($perimeter->getExternalCoverageId(), $instances);
            } else {
                throw new NotFoundHttpException("Coverage not found: " . $perimeter->getExternalCoverageId() . " in tyr api (" . $this->tyrUrl . ") ");
            }
        }
        return $userPerimeterNavitiaIds;
    }
948766
public function getStopPoints($coverageId, $networkId, $lineId, $routeId)
    {
        $pathFilter = 'networks/'.$networkId.'/lines/'.$lineId.'/routes/'.$routeId;

        $query = array(
            'api' => 'coverage',
            'parameters' => array(
                'region' => $coverageId,
                'action' => 'route_schedules',
                'path_filter' => $pathFilter,
                'parameters' => '?depth=0',
            ),
        );

        return $this->navitia_component->call($query);
    }
1033367
public function add(CoverageEntity $coverage)
    {
        if ($coverage->contains($this->lineRange) === false) {
            throw new LineOutOfRangeException($coverage, $this->lineRange);
        }
        $this->lineCoverages->set($coverage->getLineNumber(), $coverage);
    }
213544
protected function prepareListReportRoute(array &$routes)
    {
        $permissions = $this->getPermissionsReportRole();

        foreach ($routes as $pattern => &$route) {

            if (strpos($pattern, 'admin') === 0 && !isset($route['access'])) {
                $route['access'] = 'admin';
            }

            if (!isset($route['access'])) {
                $route['access'] = GC_PERM_PUBLIC;
            }

            $access_names = array();

            if (isset($permissions[$route['access']])) {
                $access_names[$route['access']] = $this->text($permissions[$route['access']]);
            } else {
                $access_names[''] = $this->text($permissions[GC_PERM_PUBLIC]);
            }

            if ($route['access'] === GC_PERM_SUPERADMIN) {
                $access_names = array(GC_PERM_SUPERADMIN => $this->text($permissions[GC_PERM_SUPERADMIN]));
            }

            $route['pattern'] = $pattern;
            $route['access_name'] = implode(' + ', $access_names);
            $route['access'] = implode('', array_keys($access_names));
            $route['status'] = !isset($route['status']) || !empty($route['status']);
        }
    }
1020320
public function getItemsPermissions($names)
    {
        $permissions = array();

        $items = $this->getPermissions();
        $flat = $this->flattenPermissions($items);

        foreach ($flat as $itemName => $item) {
            if (in_array($itemName, $names)) {
                $permissions[$itemName] = $item;
            }
        }

        return $permissions;
    }
278352
public function syncPermissions(array $permissions)
    {
        foreach ($this->permissions as $current)
        {
            /** @var Permission $current */
            if ($current->isAllowed() && ! in_array($current->getName(), $permissions))
            {
                $this->removePermission($current);
            }
        }

        $this->allow($permissions);
    }
1076028
protected function _coverage($file, $coverage)
    {
        $result = [];
        $root = $this->parse($file);
        foreach ($root->lines['content'] as $num => $content) {
            $coverable = null;
            foreach ($content['nodes'] as $node) {
                if ($node->coverable && $node->lines['stop'] === $num) {
                    $coverable = $node;
                    break;
                }
            }
            if (!$coverable) {
                continue;
            }
            if (isset($coverage[$num])) {
                $result[$num] = $coverage[$num];
            } elseif (isset($coverable->lines['begin'])) {
                for ($i = $coverable->lines['begin']; $i <= $num; $i++) {
                    if (isset($coverage[$i])) {
                        $result[$num] = $coverage[$i];
                        break;
                    }
                }
                if (!isset($result[$num])) {
                    $result[$num] = 0;
                }
            } else {
                $result[$num] = 0;
            }
        }
        return $result;
    }
734083
public function flattenPermissions($permissions)
    {
        $flattened = array();
        foreach ($permissions as $itemName => $itemPermissions) {
            $flattened[$itemName] = $itemPermissions;

            if (isset($itemPermissions['children'])) {
                $children = $itemPermissions['children'];
                unset($itemPermissions['children']); // not needed in a flat tree
                $flattened = array_merge($flattened, $this->flattenPermissions($children));
            }

            if (isset($itemPermissions['parents'])) {
                $parents = $itemPermissions['parents'];
                unset($itemPermissions['parents']);
                $flattened = array_merge($flattened, $this->flattenPermissions($parents));
            }
        }
        return $flattened;
    }
1237281
protected function filterLayoutLayer(array $presences, array $parentKeys = [])
    {
        $remove = [];

        foreach ($presences as $key => $presence) {

            if ($presence->type() !== MenuPresenceType::GROUP) {

                if ( ! $this->userHasPermission($presence->permissions())) {
                    $remove[] = $key;
                }
                continue;
            }

            // Use the permissions index to determine whether we need to analyse
            // the children of this node: not if all children are unconditional
            // (index is empty array) or user has all permissions.

            $combinedParentKeys   = $parentKeys;
            $combinedParentKeys[] = $key;

            $index = $this->permissionsIndex->getForNode($combinedParentKeys);

            if (is_array($index) && ( ! count($index) || $this->userHasAllPermissions($index))) {
                continue;
            }

            $childKeys = $parentKeys;
            array_push($childKeys, $key);

            $presence->setChildren(
                $this->filterLayoutLayer($presence->children(), $childKeys)
            );

            if ( ! count($presence->children())) {
                $remove[] = $key;
            }
        }

        if (count($remove)) {
            array_forget($presences, $remove);
        }

        return $presences;
    }
199834
static function fromCoverage(string $coverage): self {
    $report = new static;
    $records = $report->getRecords();

    try {
      /** @var Record $record */
      $record = null;
      foreach (preg_split('/\r?\n/', $coverage) ?: [] as $line) {
        $line = trim($line);
        if (!mb_strlen($line)) continue;

        $parts = explode(':', $line);
        if (count($parts) < 2 && $parts[0] != Token::endOfRecord) throw new \DomainException('Invalid token format');

        $token = array_shift($parts);
        $data = explode(',', implode(':', $parts));
        $length = count($data);

        switch ($token) {
          case Token::testName:
            $report->setTestName($data[0]);
            break;

          case Token::sourceFile:
            $record = new Record($data[0], new FunctionCoverage, new BranchCoverage, new LineCoverage);
            break;

          case Token::functionName:
            if ($length < 2) throw new \DomainException('Invalid function name');
            if ($functions = $record->getFunctions()) $functions->getData()->append(new FunctionData($data[1], (int) $data[0]));
            break;

          case Token::functionData:
            if ($length < 2) throw new \DomainException('Invalid function data');
            if ($functions = $record->getFunctions()) foreach ($functions->getData() as $item) {
              if ($item->getFunctionName() == $data[1]) {
                $item->setExecutionCount((int) $data[0]);
                break;
              }
            }
            break;

          case Token::functionsFound:
            if ($functions = $record->getFunctions()) $functions->setFound((int) $data[0]);
            break;

          case Token::functionsHit:
            if ($functions = $record->getFunctions()) $functions->setHit((int) $data[0]);
            break;

          case Token::branchData:
            if ($length < 4) throw new \DomainException('Invalid branch data');
            if ($branches = $record->getBranches()) $branches->getData()->append(new BranchData(
              (int) $data[0],
              (int) $data[1],
              (int) $data[2],
              $data[3] == '-' ? 0 : (int) $data[3]
            ));
            break;

          case Token::branchesFound:
            if ($branches = $record->getBranches()) $branches->setFound((int) $data[0]);
            break;

          case Token::branchesHit:
            if ($branches = $record->getBranches()) $branches->setHit((int) $data[0]);
            break;

          case Token::lineData:
            if ($length < 2) throw new \DomainException('Invalid line data');
            if ($lines = $record->getLines()) $lines->getData()->append(new LineData(
              (int) $data[0],
              (int) $data[1],
              $length >= 3 ? $data[2] : ''
            ));
            break;

          case Token::linesFound:
            if ($lines = $record->getLines()) $lines->setFound((int) $data[0]);
            break;

          case Token::linesHit:
            if ($lines = $record->getLines()) $lines->setHit((int) $data[0]);
            break;

          case Token::endOfRecord:
            $records->append($record);
            break;
        }
      }
    }

    catch (\Throwable $e) {
      throw new LcovException('The coverage data has an invalid LCOV format', $coverage, -1, $e);
    }

    if (!count($records)) throw new LcovException('The coverage data is empty', $coverage);
    return $report;
  }
1305761
public function getMask(array $permissions)
    {
        $maskBuilder = new MaskBuilder();

        foreach ($permissions as $permission) {
            try {
                $maskBuilder->add($permission);
            } catch (InvalidArgumentException $e) {
                throw new InvalidPermissionException('Invalid permission mask: '.$permission, $permission, $e);
            }
        }

        return $maskBuilder->get();
    }
255228
protected function checkPermissions(array &$permissions) {
    $available = array_keys($this->getAllPermissions());

    foreach ($permissions as $permission) {
      if (!in_array($permission, $available)) {
        throw new \RuntimeException(sprintf('Invalid permission "%s".', $permission));
      }
    }
  }
110786
protected function addRoles($roles)
    {
        if (!is_array($roles) && !($roles instanceof \Traversable)) {
            $roles = array($roles);
        }

        /* @var $role Role */
        foreach ($roles as $role) {
            if ($this->acl->hasRole($role)) {
                continue;
            }

            if ($role->getParent() !== null) {
                $this->addRoles(array($role->getParent()));
                $this->acl->addRole($role, $role->getParent());
            } elseif (!$this->acl->hasRole($role)) {
                $this->acl->addRole($role);
            }
        }
    }
369547
public function assignProfiles($profiles) : void
    {
        $profiles = (array) $profiles;
        foreach ($profiles as $profile) {
            $electedProfile = Profile::where('code', $profile)->first();

            // Profile exists and not assigned to user?
            if (!is_null($electedProfile) && !$this->hasProfile($profile)) {
                // Assign both role and permissions defined for this user profile.
                // Into the Spatie Permissions.
                $this->assignRole(Role::find([$electedProfile->role_id])->first()->name);
                $this->givePermissionTo(Permission::find([$electedProfile->permission_id])->first()->name);

                // Assign user profile into the profiles table.
                $this->profiles()->save($electedProfile);
            }
        }
    }
1010596
public function setAccessRoles(array $accessRoles)
    {
        foreach ($accessRoles as $role) {
            if (!is_string($role)) {
                throw new \InvalidArgumentException('The role names passed to setAccessRoles() must all be of type string.', 1302172892);
            }
        }
        if ($this->accessRoles !== $accessRoles) {
            $this->accessRoles = $accessRoles;
            $this->addOrUpdate();
        }
    }
468877
public function getStopPoint($coverageId, $stopPointId, $params)
    {
        $pathFilter = 'stop_points/'.$stopPointId;
        $parameters = http_build_query($params);

        $query = array(
            'api' => 'coverage',
            'parameters' => array(
                'region' => $coverageId,
                'path_filter' => $pathFilter,
                'parameters' => $parameters,
            ),
        );

        return $this->navitia_component->call($query);
    }
541157
private function scanVendorFolders(&$folders)
    {
        $basedir = realpath(NABU_PROVIDERS_PATH);
        if (is_dir($basedir) && ($h = opendir($basedir))) {
            while (($folder = readdir($h))) {
                if ($folder !== '.' && $folder !== '..') {
                    $this->scanModuleFolders($basedir . DIRECTORY_SEPARATOR . $folder, $folders);
                }
            }
            closedir($h);
        }
    }