private function openFile($file)
    {
        if (is_resource($file)) {
            if (get_resource_type($file) !== 'stream') {
                throw new \InvalidArgumentException('The given resource is not a file handle.');
            }
        } else {
            $filename = (string) $file;

            if (!is_file($filename) || !is_readable($filename)) {
                throw new \LogicException(sprintf('File "%s" could not be opened.', $filename));
            }

            $file = fopen($filename, 'r');
        }

        return $file;
    }