private function topologicalSort($operations, $dependency)
    {
        $sorted = $arranged = [];
        $deps = $dependency;

        while ($deps) {
            $noDeps = [];

            foreach ($deps as $index => $dep) {
                if (!$dep) {
                    $noDeps[] = $index;
                }
            }

            if (!$noDeps) {
                throw new ValueError('Cyclic dependency on topological sort');
            }

            $arranged = array_merge($arranged, $noDeps);

            $newDeps = [];
            foreach ($deps as $index => $dep) {
                if (!in_array($index, $noDeps)) {
                    $parents = array_diff($dep, $noDeps);
                    $newDeps[$index] = $parents;
                }
            }
            $deps = $newDeps;
        }

        foreach ($arranged as $index) {
            $sorted[] = $operations[$index];
        }

        return $sorted;
    }